Category Archives: Ecosystems

Sonoff POW Elite – power meter with relay, WiFi. Up to 20A

Approximately a week ago, in the official internet shop of a Chinese company Sonoff two devices appeared, marked as POWR316D and POWR320D. It’s the same device, with two modifications that differ only in the maximum allowable current flowing through it. And these are 16 and 20 Amps. I will review the devices as one, and the user must know the difference between the models when ordering the device.

SONOFF POW Elite features

  • Model – POWR316D/POWR320D
  • Case material – PC V0
  • Dimensions – 98x54x31 mm (DIN Rail mountable)
  • Input/Output – 100-240 В 50/60 Hz, 16A/20A Max
  • Wi-Fi – IEEE 802.11 b/g/n 2,4 GHz
  • WiSoC – Espressif ESP32 240 MHz with connection to Wi-Fi 2,4 GHz and Bluetooth 4.2

From the specifications, it is clear that the “brain” of the device is already familiar to us ESP32. Theoretically, we can change its firmware to alternatives called Tasmota or ESPhome.
The power meter has the possibility of mounting on a standard DIN rail.
You can install several such devices in an electrical switchboard and control the supply voltage, for example, outlets in your home. In this case, you should ensure a stable Wi-Fi signal between your router and the power meter in the shield. After all, its metal body can shield the passage of radio waves between devices. I will also warn readers against such a power meter at the main entrance to the apartment or house since its allowable power will not be enough for all your appliances.

Terminals for connection for both input and voltage output wires are in the lower part of the housing. There are four terminals, and you clamp the wires with screws. To access the clamping screws, remove the cover on the bottom of the housing. On the case near screws, there are markings for the corresponding wires.

At the top of the case above the display are status indicators with a single button to control the relay. This button also performs a device reset function.

eWeLink app features

The program interface is the same for almost all switches. Here’s quick access to the relay state switch from the card and the main menu. Detailed information on power consumption is also displayed here. The following information will be available to users

  • Electricity consumption for today (kWh)
  • Electricity consumed yesterday (kWh)
  • Electricity consumed per month (kWh)
  • Current (A)
  • Voltage (V)
  • Power (W)

The settings section will show the power status, we can turn on local control, and we can add schedules, timers, and cycles. Other settings allow you to add notifications based on relay status or power consumption goals for the day or month.

The values on the display and in eWeLink are updated every 5 seconds.

Smart Controls

eWeLink is compatible with smart speakers from Alexa and Google Home.
There is a possibility of integration with SmartThings. But unfortunately, you can’t control anything but turning the relay on or off. It is not possible to obtain information on electricity consumption through such integrations. There will be alternative firmware for the device. It’s probably already made. Both Tasmota and ESPHome allow you to transfer power usage statistics to Home Assistant, and from there, you can send them to Alexa or SmartThings.

What’s inside

Usually, Sonoff devices come with everything needed to tinker with the firmware, and the Sonoff POW Elite is no different. The PCB contains contacts for ESP32 (TC, RX, GND, Vcc), and something tells me that the button links to GPIO00, which enables the firmware mode.

The antenna got printed to the PCB. Unfortunately, there is no connector for connecting an external antenna, which makes another argument against a weak Wi-Fi signal.

The display controller chip is on the same side as the ESP32, with several contacts next to it to connect to the display itself. By the way – the screen has no backlight, which can be inconvenient at night. On the other hand, few control electricity consumption in the middle of the night.
ЗOn the back of the PCB, we see the elements of the power supply system (small transformer, capacitors, and filters). The large item on this side of the board is the relay.

This relay exactly controls the supply of voltage to the output of the power meter. In my opinion, the manufacturer could install a more powerful relay because, as you can see in the photo, there is more than enough space on the board. Maybe in a while, we will see an updated model of this power meter from Sonoff, but already designed for more power. It would be interesting to install one at the entrance to your home to control the electricity consumption inside your home. Logically, such a power meter would cost more, but I think the price here would differ only due to the cost of a more powerful relay.

Installing Ukraine Alarm in HA and setting up notifications

Update 2022.5.3 to Home Assistant introduces a new integration called Ukraine Alarm. With its help, you can get information about air alarms or other alarms in Ukraine in real-time.

Ukraine Alarm creates four binary sensors, each responsible for a specific type of alarm, and when the sensor is in the “on” state, a corresponding alarm goes off.

In version 2022.5.4 HA, the integration got an update. Now you do not need to wait for the API key from the developers (who may not give you the key), and you can configure “Ukraine Alarm” by specifying the area you plan to monitor immediately.

Adding Ukraine Alarm integration to Home Assistant

Click the My button or add “Ukraine Alarm” manually by clicking the “+ Add Integration” button and start entering the name. Then click on the name of our integration.

Next, you need to choose the area where you plan to monitor the alarm, which could be an area, district, local community, or city.

In the last step, you can assign a room (zone) to which new integration sensors would add.

After clicking on the “Finish” button, the “Ukraine Alarm” integration will be added.

As you can see in the image, each instance of this integration creates four binary_sensor entities responsible for having a type of alarm. These alarms are Air, Artillery, Urban Fights, and Unknown. There is a limit on the number of territories, not to exceed the number of API requests from one IP. You can monitor up to 5.

Adding sensors to the home screen in the Dashboard

If you do not want to receive notifications but want clear information about alarms in a particular region, add sensors to the Lovelace card. Of course, you can design the look to your liking.

The first image shows a card with four-alarm sensors when there are no alarms. The second image shows the state of the sensors when the air alarm goes off.

Adding sensors to the home screen in the Dashboard

I already made a tutorial on setting up a Telegram bot that receives notifications from your smart home. Next, I’ll just add the automation code, which will allow you to send notifications to your telegram bot.

I will also provide the code to allow you to manage notifications in your Home Assistant mobile app. This example will use a voice notification, and you will hear it even when your phone is silent. Well, let’s get acquainted with the code.

In the code above, I left comments on the relevant parts.

That’s all for today. Have a peaceful sky without air alerts!

Meeting the NSPanel switch and flashing ESPHome

Last year, the Chinese company Sonoff started crowdfunding, where they presented an interesting smart device. By the end of 2021, Sonoff introduced NSPanel. This device is a rectangular device with a colored touch display sized 3,5 inches and two physical buttons.

The touch display was also created by Sonoff, this is a Nextion model.

There are two versions of the NSPanel, and they differ only in size. One is for the US market, and the other one is for the EU countries. The power and logical parts of the devices are the same. As a “brain“, the company chose the Espressif ESP32-DOWD V3, which allowed working in Wi-Fi 4 802.11n/g/n networks. Bluetooth 4.2/5.x is only used when setting up the device.

Connecting NSPanel to the power grid

To set up and control the device you need to download the eWeLink app, it must be familiar with Sonoff devices owners. If you do not have an account yet, create it, or log onto your account. eWeLink lets you control connected devices from all around the world where the Internet connection is established.

So, to connect the NSPanel on the phone you need to turn on Bluetooth. After that, connect the device to the power grid.

Use safety precautions when working with high voltages!

In the eWeLink app press “add new device“. Then, you will see the device on the screen, tap on it to add it. Then, you may need to enter your Wi-Fi credentials. After connecting, the NSPanel most likely will ask you to update its firmware. Do it, because this update may contain bug fixes or performance improvements. The update may take a while.

The power part of the switch includes two relays that can withstand up to 16A of current. This means that you can connect two additional electric devices. It could be not only lighting devices but also, for example, a warm floor controller. Below is a switch connecting scheme.

Setting up the NSPanel in eWeLink

In the eWeLink application, you can switch one of the switch relays to thermostat mode, then the built-in thermal sensor can control the temperature in the room. For the thermostat, choose the heating or cooling mode depending on the device you want to connect (heater or cooler). Set the target temperature in the room and the device will turn off when it reaches the target temperature.

To reach the thermostat screen on the switch, swipe right on its screen. One more swipe will lead you to widgets. These are pictograms with which you can control other smart devices. This way you can control plugs or bulbs, and also their groups. In addition, you can create widgets to load a scene.

You need to create widgets in the eWeLink app. The max widget number is 8.

To control the device using voice assistants create the appropriate integration in the eWeLink app. In addition, Sonoff allows controlling the switch in a local network, which enables integrations into alternative smart home control systems. Enable the corresponding item in the device settings.

Getting ready to launch ESPHome

I think that this smart device is “smart” only when it can work independently of cloud services. NSPanel can work locally but sadly, its widgets don’t work without a network connection. Essentially, you can only control two relay devices through a local network.

Just as I stated above, the logical part of the switch is a Nextion display that is connected to the esp32 controller. Such a tandem gives us hope for the ability to easily load alternative firmware. And really, “tasmota” firmware already exists for our device, work on creating an NSPanel component for ESPhome is also in full swing.

Let’s look at the PCB of the logic unit. To take it out, lightly press the screwdriver on the groove in the center. Remember to turn off the power beforehand!

Now you need to unscrew two screws that hold the protective plate. After you carefully remove the plate, you will see the PCB with the esp32 controller microcircuit.

You will see 5 contacts in the lower corner. They are marked as 3V3, ESP_TX, ESP_RX, GND, and IO0. The last contact is a “zero” pin controller. To switch the controller to firmware mode, connect it to the neutral wire.

To flash the controller’s firmware, you will need a usb2uart (USB to TTL converter) adapter. Before you connect it to the computer, set the voltage to 3.3V, this is important!

Using Dupont wires, connect the adapter to the PCB: VCC – 3V3, GND – GND, RX – ESP_TX, TX – ESP_RX. Connect the IO0 pin on the PCB to the neutral wire on the contact pad (green wire on the picture)

Pay attention: If the Dupont wire contacts protrude beyond the contact holes on the board, they will touch the metal plate of the display. This can lead to a short circuit and both the USB adapter and NSPanel chips failure! To avoid this, disconnect the display cable and remove the PCB from the chassis.

Downloading ESPHome onto the NSPanel

Now you can confidently download the ESPHome firmware. Connect the USB adapter to the computer, and in the device manager, look what COM port it received. Now, in the ESPHome interface, you can create a new node. To be honest, downloading empty code onto esp32 and then working with the created node using Wi-Fi would be enough. To do this, select the firmware option of the ESP controller connected to the computer. Then, select the COM port in the popup. Downloading the firmware takes a few minutes. The contacts need to stay connected!

After the firmware is loaded, disconnect the wires and assemble the switch. After connecting the NSPanel to the network, you can use the switch just like a regular esp32 controller. Now let’s download an already prepared code.

After reboot, the switch will have a familiar interface. I want to warn you: There’s no official thermostat support, so I didn’t add it in the code. The thermostat, just like some widgets, could be added by JSON requests. The format which JSON should have can be read here. And here you can find another variant of the code, already bundled with some widgets and a thermostat. One thing, you will need to replace the device IDs with yours.

For an example of scene widgets, I added code that will turn off melodies on the built-in NSPanel booster (piezodynamic). You can add and load any scene with Home Assistant.

After the official release of NSPanel components for ESPHome, I plan to update the article, so check for updates. In addition, there is a way to fully change the interface and switch from the NSPanel Protocol, since onboard of the switch we have Nextion display and esp32, but that is a topic for another article.

Setting up the integration for Nest devices in Home Assistant

A little over a month ago, I made a review on the Nest Learning Thermostat. It is a handy device that will support the comfortable temperature inside your room and will save your money on heating at the same time.

As you know, Google owns Nest, and how it often happens in big companies, not all areas of production keep up with user requests. And the fact that Google has partially closed the Nest API has been sad news for independent developers. If you use the Nest app, which manages only its own devices, you may have paid attention to the fact that the app did not update for quite a while. It also does not have that many devices.

I think it will be much more convenient for anyone to control their devices from within one app. I chose Home Assistant as the system since you can integrate many devices from different brands, manage them, and add any automatization algorithm.

I must warn you: to get access to the Nest Smart Device Management API you will need to pay 5 dollars. It is a one-time payment for API access.

After finishing setup through Home Assistant, you will gain access to these essences: Climate, Sensor, Camera.

Registering device access

For this step, enable Nest API and create an account for Home Assistant to exchange info with the Nest API.

Creating and configuring Cloud Project in the Cloud Console:

  • Go to Google Cloud Console.
  • If this is your first time, you need to create a new project in Google Cloud. Click on “Create Project”, and then “New Project”.

  • Name your project and click on “Create”.
  • Go to “APIs & Services > Library”. Here you can enable the API.
  • Search for “Smart Device management” and click on “Enable”.

  • Now enter “Cloud Pub / Sub API” in the API library search box and click “Enable.”

Now you have a project ready to set up authentication with OAuth.

Setting up OAuth in Cloud Console Consent Screen

In this section, you configure the OAuth consent screen. It is needed to give Home Assistant access to your project.

  • Go to Google API Console.
  • Click on “OAuth consent screen” and start setting it up.
  • Choose “External” (this is the only option if you are not a G-Suite user), and then click on “Create”.

  • On the “App Information” screen, enter the App name and the User Support E-Mail, and then type in your E-Mail address in the “Developer contact email” section. They are displayed only during the OAuth to give Home Assistant access to your account. Click on “Save” and “Continue”. Do not add unnecessary info (for example, the logo) to avoid additional Google checks.
  • On the “Scopes” step, click on “Save and Continue”.
  • On the “Test Users” step, add your Google account (example@gmail.com) to the list. Click on “Save” in your test account and then “Save and Continue” to finish the process.

  • Go back to the OAuth consent screen and click “Publish App” to set the “In Production” status.

  • The warning states that your application will be available to any user with Google Accounts that you have entered on the application information screen. It does not compromise your Google Account or your Nest data.
  • Make sure to disable the “Testing” status since you will be logging out of your Google account every week.

Configuring OAuth client_id and client_secret in the Cloud Console

At the end of this section, you will get the client_id and client_secret you need for the next steps.

  • Go to the “Credentials” page and click on “Create Credentials”.

  • Select “OAuth client ID” in the drop-down list.

  • Choose “Desktop App” in the “Application type” type field.
  • Give an understandable name to your credentials.
  • You will now see a message created by the OAuth client. Pay attention to “Client ID” and “Client Secret”. They are crucial to the next steps.

Creating device access for project_id in the Device Access Console

Now that you have set up the authentication, create the Nest Device Access Project, which requires $5. When done, you will have device access to project_id, which is crucial for further steps.

  • Go to the “Device Access Registration” page. Click on the “Go to the Device Access Console” button.
  • Check the “Accept the Terms of Service” and click on “Continue to Payment” where you will need to pay 5 dollars.

  • The “Device Access Console” should now be displayed. Click on “Create project”.
  • Give a name to your project and click on “Next”.

  • You then are prompted to enter the OAuth client ID you created earlier in the previous section. Click on “Next”.

  • Turn on “events” by clicking on “Enable and Create project”.

  • Note the Project ID. You currently have project_id, client_id, and client_secret required for Home Assistant.

Setting up Home Assistant

Now you have everything required to set up Nest in Home Assistant. Open “configuration.yaml” and write according to the sample you see below.

# Example configuration.yaml entry
nest:
  client_id: CLIENT_ID
  client_secret: CLIENT_SECRET
  # "Project ID" in the Device Access Console (not Cloud Project ID!)
  project_id: PROJECT_ID

Be sure to restart Home Assistant in the “Server Controls” section.

Setting up devices

After you added a Nest configuration entry to “configuration.yaml” and rebooted Home Assistant, you need to add Nest integration through the user interface. To do this, you can use the “My” button:

You can do the same manually:

  1. Go to your Home Assistant interface.
  2. On the sidebar, click on “Configuration”.
  3. In the configuration menu, select “Integrations”. (in version 2021.12 Devices & Services)
  4. In the lower right corner, click the “Add Integration” button.
  5. Use the search box, select “Nest” and follow the instructions.

Setting up the Nest integration will guide you through the steps of authorizing your Home Assistant to access your account on Nest devices.

Setting up OAuth for Device Authorization

This section will allow Home Assistant to access your account by generating an OAuth token.

  • Select “OAuth or Apps”, because we created credentials for the computer programs above in the Google Cloud Console.

  • Click the “authorize your account” link.

  • A new tab will open where you can select your Google Account. It should be the same developer account you set up before.
  • The Google Nest permissions screen will allow you to choose which devices to configure and select from several of your homes. Maybe you want to turn on everything. However, you can skip any feature you do not need to use.

  • You will get redirected to another account selection page.
  • You may see a warning screen stating that Google has not tested this program because you have just set up an unverified developer workflow. Click on “Continue” to continue.

  • You will then get prompted to grant additional permissions. Click on “Allow”.
  • Confirm that you want to grant permanent access to Home Assistant.

  • Copy the token.

  • Insert the token into the Google Home Assistant Link Account dialog box.

  • The next step is to enter the Cloud Project ID to receive updates from your devices. Open the Cloud Console and copy the Project ID.

If everything is as planned, you will see a working Nest integration.

Using automation and triggers

Nest integration makes device triggers available and the ability to enable automation in Home Assistant. For complete information, see the Home Assistant Automation Guide.

You can use nest / set_away_mode to set Home or Away mode:

# Example script to set away, no structure specified so will execute for all
script:
  nest_set_away:
    sequence:
      - service: nest.set_away_mode
        data:
          away_mode: away

Controlling the thermostat

The Nest climate platform allows you to control the thermostat from Nest. However, please note that due to the limitations of the European Nest E thermostat, integration with the Home Assistant is not possible for this thermostat.

After setting up the integration and adding the thermostat card to the Lovelace interface, you will be able to control your Nest devices via Home Assistant.

Also, it is now possible to add new climate entities in triggers and automation, calling the appropriate service using previously created configuration files. You can view the templates in the Home Assistant developer tools.

How to clean up the Home Assistant configuration file

Once upon a time, there was complete chaos in my Home Assistant configuration files, and it took a while before I put them in order. The configuration.yaml file was oversaturated with code and disorganized. My automations were in one file, and I have over 100 automations. All of my scripts were in a similar situation. Over time, I’ve created dozens of templates for sensors and binary_sensor, all in their “configuration.yaml” files. After getting confused about the configuration and settings, I decided on “general cleaning.”

Preamble

I should note that this article isn’t a discussion of the best ways to organize Home Assistant configuration files. Here are some points to keep in mind. I will focus on the configuration process for Home Assistant and what and how I decided on specific settings. I hope my experience can help you.

The initial state of Home Assistant configuration files

I have been using Home Assistant for almost three years. During this time, I’ve developed and expanded the capabilities of Home Assistant, making it a powerful tool for managing my smart home. Though, I didn’t plan or shorten my configuration files.

All my automations were in one big file (automations.yaml). Also, all my scripts were in one file (scripts.yaml). My configuration.yaml included a lot of sensors, lamps, switches, and more. It became difficult to find and manage files.

I wanted to make each of the configuration items more manageable. I didn’t want to have to scroll and search through long files to make minor edits. I want to make it easier to track errors in my configurations. I need to understand where to add new code.

Settings of the Home Assistant configuration file

Home Assistant has several code-splitting options. Their documentation explains these options in detail.

You can use “include” to move all of the integration to a new separate file(s).
You can use more advanced “include” versions to move full integrations to folders with files.
Examples will help in explaining those options.

The basics of the include setting

Let’s begin with the most simple example:

automation: !include automations.yaml

This line placed in your configuration.yaml file tells Home Assistant that your code is in the file called automations.yaml in the same configuration folder. Similarly, you can split more parts of your configuration.yaml file:

automation: !include automations.yaml
switch: !include switches.yaml
light: !include lights.yaml

The example above moves the code for automation, switches, and lighting devices to separate files.

Advanced include options

For advanced options, I will copy the explanation from the Home Assistant documentation:

  • !include_dir_list will return the contents of a directory as a list. The contents of each file will be an entry in the list. The list entries are ordered based on the alphanumeric order of the file names.
  • !include_dir_named will return the contents of a directory as a dictionary that displays filename => file contents.
  • !include_dir_merge_list will return the contents of a directory as a list by merging all the files into one list.
  • !include_dir_merge_named returns the contents of a directory as a dictionary, loading each file and merging them into one dictionary.
    Let’s look at an example of each of them:
automation: !include_dir_list automations/

In this case, each automation would be in its YAML file inside the automations/ directory. So you can have turn_on_lights.yaml and turn_off_lights.yaml files in the automations directory, each with its automation.

Compare this with:

automation: !include_dir_merge_list automations/

In this case, each YAML file in the automations/ directory can have multiple automations inside. You can write rules for turning lights on and off in a file called lights.yaml. The thing to remember when using the merge_list directive is that all automations in all files must be in list format (hyphenated), for example:

- alias: "Turn on Light"
  trigger:
    - platform: state
      entity_id: device.iphone
      to: "home"
  action:
    - service: light.turn_on
      target:
        entity_id: light.living_room

but not:

alias: "Turn on Light"
trigger:
  - platform: state
    entity_id: device.iphone
    to: "home"
action:
  - service: light.turn_on
    target:
      entity_id: light.living_room

!include_dir_named and !include_dir_merged_named work similarly, but instead of merging files into a list, they combine into a dictionary. For example, scripts load into a dictionary. Use an entry instead of a list like this:

script: !include_dir_named scripts/

to combine files in the scripts directory, each containing one script, or

script: !include_dir_merge_named scripts/

to combine files in the scripts directory, which could contain more than one script.

What I decided to change

I used a combination of include directives to split up my configuration file and organized my automations as a merged list. This way, you can store automation groups in one file. For example, I have irrigation.yaml file for irrigation automation and an hvac.yaml file for heating and cooling automation. I also created a unified dictionary for my scripts.

For sensors, lamps, switches, groups, shell commands, and input_numbers, I used a simple include directive to keep them in their files. I don’t tweak and add and change them as often as automations, and there aren’t that many, so I think a separate file for each is fine. Here is the relevant section of my configuration.yaml:

group: !include groups.yaml
automation: !include automations.yaml
automation mine: !include_dir_merge_list automations/
script: !include_dir_merge_named scripts/
sensor: !include sensors.yaml
binary_sensor: !include binary_sensors.yaml
switch: !include switches.yaml
light: !include lights.yaml
rest_command: !include rest.yaml
remote: !include remote.yaml
shell_command: !include shell.yaml
input_number: !include input_number.yaml

Note lines 2 and 3 above. I have a regular include directive and a merge list directive for automations, which allows me to organize the automations I write into files while the automations generated by the UI will still be in automations.yaml.

But what about packages?

Another way to organize your files is to use packages. Packages allow you to bundle integrations (like light, switch, input_boolean, etc.) together. You can combine packages with include directives to group functions in your configuration more logically. For example, you can have a home theater package that will include all the lighting, audio-video equipment, and automation for the room in one package.

Last thoughts

I hope that after this article, you will have new ways to organize your Home Assistant’s configuration files. I’d love to hear how you do it. Let me know in the comments.