Sonoff Refresh Automation

I use the automation below to update or refresh a bunch of Sonoff TH temp/humidity sensors.

I keep adding more sensors and would like to not have to add the sensor entity to the automation each time (mostly because I forget to do it).

Is there an auto-entity or wildcard kind of way to make an automation to refresh all the Sonoff sensors?

alias: Sonoff Refresh All Sensors
description: ""
trigger:
  - platform: time_pattern
    minutes: /1
    hours: "*"
action:
  - service: homeassistant.update_entity
    target:
      entity_id:
        - switch.sonoff_1001392b9f
        - switch.sonoff_1001829e6f
        - switch.sonoff_1001392bce
        - switch.sonoff_1001392bd9
        - switch.sonoff_10013933f8
        - switch.sonoff_1001393418
        - switch.sonoff_100139392c
        - switch.sonoff_100139390d
        - switch.sonoff_10013932b1
        - switch.sonoff_1001829e6f
        - switch.sonoff_1001829f54
        - switch.sonoff_1001829f81
        - switch.sonoff_1001829fce
        - switch.sonoff_1001829fd2
        - switch.sonoff_100182a1cc
        - switch.sonoff_1001dabe7c
        - switch.sonoff_1001dabf2f
        - switch.sonoff_1001dabf37
        - switch.sonoff_1001dabec9
        - switch.sonoff_1001dac0ed
        - sensor.125_1st_floor_temperature
        - sensor.125_2nd_floor_temperature
        - sensor.1st_floor_room_temperature
        - sensor.2nd_floor_east_room_temperature
        - sensor.2nd_floor_west_room_temperature
        - sensor.371_1st_floor_temperature
        - sensor.371_2nd_floor_ecobee_smartsensor_temperature
        - sensor.371_2nd_floor_temperature
        - sensor.371_bedroom_temperature
        - sensor.371_hydronic_temperature
        - sensor.371_large_bedroom_temperature
        - sensor.3rd_floor_room_temperature
        - sensor.630_temperature
        - sensor.76_temperature
        - sensor.76_thr316_1_water_temperature
        - sensor.carmine_temperature
        - sensor.hoophouse_shelly_uni_tasmota_1_am2301_temperature
        - sensor.shop_temperature
        - sensor.sonoff_1001392b9f_temperature
        - sensor.sonoff_1001392bce_temperature
        - sensor.sonoff_1001392bd9_temperature
        - sensor.sonoff_10013932b1_temperature
        - sensor.sonoff_10013933f8_temperature
        - sensor.sonoff_1001393418_temperature
        - sensor.sonoff_100139390d_temperature
        - sensor.sonoff_100139392c_temperature
        - sensor.sonoff_1001829f54_temperature
        - sensor.sonoff_1001829f81_temperature
        - sensor.sonoff_1001829fce_temperature
        - sensor.sonoff_100182a1cc_temperature
        - sensor.sonoff_1001dabe7c_temperature
        - sensor.sonoff_1001dabf2f_temperature
        - sensor.sonoff_1001dac0ed_temperature
        - sensor.th316_125_temperature
        - sensor.th316_76_temperature
        - sensor.thr316_3_temperature
        - sensor.well_ecobee_temperature
        - sensor.sonoff_1001dbcdc2_humidity
        - sensor.sonoff_1001dbcdc2_temperature
    data: {}
mode: single

Not really, It is because the Sonoff stuff only builds the database on a HA startup. From reading the Sonoff HACS docs

Any reason not to use esphome and the superior native api on compatible devices?

Aren’t sonoff devices even cloud polling (the worst possible)? Local push with native api will be just luxury pure for you then!

Please forgive me if I misunderstand or misuse terminology.

The Sonoff integration I am using is:

GitHub - AlexxIT/SonoffLAN: Control Sonoff Devices with eWeLink (original) firmware over LAN and/or Cloud from Home Assistant

The mode is AUTO (not CLOUD or LOCAL).

I think I need a cloud-based solution because these sensors are scattered over quite a number of physical locations, all, “network-wise,” remote from the HA server.

I don’t understand what this means: “Sonoff stuff only builds the database on an HA startup.”

From what I understand, the Sonoff devices are added to (registered) my eWeLink account using the eWeLink app (on my iPhone), and thereby these devices become available to the SonoffLAN HA integration.

Is there a better way?

Thank you!

That is true, but it only access and builds upon a start or restart. Mine is auto and the phone app sees it after pairing. But the HACs will not see it until it restarts (because it sends your account/pw) and signs in, then it accesses the account and builds a new device list from that info. Other than that it is logged and just passing data back and forth retrieving the power, volts, watts, switch status, switch commands, etc
From the Docs in HA Sonoff:
Each time the integration starts, a list of user devices is loaded from cloud and saved locally (/config/.storage/sonoff/).

Thank you very much for explaining.

Just a quick repeat: Regardless of how it might sound, I do not claim to be speaking from a position of knowledge or authority. That said…

I think what you’re referring to is the list of devices that the eWeLink cloud maintains, and, thereby, what the AlexxIT HA integration uses to known or learn the complete list of my Sonoff devices.

My question was simpler (albeit, possibly not simpler to solve): Is there a way to use make the HA automation (that I use for refreshing the HA database with temp/humidity data from the eWeLink cloud) automatically include any newly added Sonoff devices?

Code that would accomplish this:

alias: Sonoff Refresh All Sensors
description: ""
trigger:
  - platform: time_pattern
    minutes: /1
    hours: "*"
action:
  - service: homeassistant.update_entity
    target:
      entity_id:
        <AUTOMATICALLY FIND/INCLUDE ALL SONOFF TEMPERATURE AND HUMIDITY PROVIDING DEVICES IN THE eWeLink CLOUD>
    data: {}
mode: single

Any solution to having an automation that automatically includes targets with matching entity criteria?

Something like:

action:
  - service: homeassistant.update_entity
    target:
      entity_id:
        - *sonoff*
        - *temperature*

Thanks!

Anyone have any ideas?

Copy-paste the following template into the Template Editor and let me know what it reports (if anything; I’m making a guess that your custom integration’s name is ‘sonoff’).

{{ integration_entities('sonoff') }}
1 Like

SO TOTALLY COOL!

Results:

[
  "sensor.sonoff_1001dbcdc2_temperature",
  "sensor.sonoff_1001dbcdc2_humidity",
  "sensor.sonoff_1001dabf2f_temperature",
  "sensor.sonoff_1001dabf2f_humidity",
  "sensor.sonoff_1001dabe7c_temperature",
  "sensor.sonoff_1001dabe7c_humidity",
  "sensor.thr316_3_temperature",
  "sensor.thr316_3_humidity",
  "sensor.sonoff_thr316_255_basement_1001dac0ed_temperature",
  "sensor.sonoff_1001dac0ed_humidity",
  "sensor.76_thr316_1_water_temperature",
  "sensor.76_thr316_1_water_humidity",
  "sensor.sonoff_1001829f54_temperature",
  "sensor.sonoff_1001829f54_humidity",
  "sensor.sonoff_1001829fce_temperature",
  "sensor.sonoff_1001829fce_humidity",
  "sensor.sonoff_1001829f81_temperature",
  "sensor.sonoff_1001829f81_humidity",
  "sensor.th316_76_temperature",
  "sensor.th316_76_humidity",
  "sensor.th316_125_temperature",
  "sensor.th316_125_humidity",
  "sensor.sonoff_100182a1cc_temperature",
  "sensor.sonoff_100182a1cc_humidity",
  "sensor.sonoff_10013933f8_temperature",
  "sensor.sonoff_10013933f8_humidity",
  "sensor.sonoff_10013933f8_rssi",
  "sensor.sonoff_1001392b9f_temperature",
  "sensor.sonoff_1001392b9f_humidity",
  "sensor.sonoff_10013932b1_temperature",
  "sensor.sonoff_10013932b1_humidity",
  "sensor.sonoff_100139392c_temperature",
  "sensor.sonoff_100139392c_humidity",
  "sensor.sonoff_1001393418_temperature",
  "sensor.sonoff_1001393418_humidity",
  "sensor.sonoff_1001392bce_temperature",
  "sensor.sonoff_1001392bce_humidity",
  "sensor.sonoff_1001392bd9_temperature",
  "sensor.sonoff_1001392bd9_humidity",
  "sensor.sonoff_100139390d_temperature",
  "sensor.sonoff_100139390d_humidity",
  "sensor.sonoff_100139390d_rssi",
  "switch.sonoff_1001dbcdc2",
  "switch.sonoff_1001dabf2f",
  "switch.sonoff_1001dabe7c",
  "switch.sonoff_1001dabec9",
  "switch.sonoff_1001dac0ed",
  "switch.sonoff_1001dabf37",
  "switch.sonoff_1001829f54",
  "switch.sonoff_1001829fce",
  "switch.sonoff_1001829f81",
  "switch.sonoff_1001829fd2",
  "switch.sonoff_1001829e6f",
  "switch.sonoff_100182a1cc",
  "switch.sonoff_10013933f8",
  "switch.sonoff_1001392b9f",
  "switch.sonoff_10013932b1",
  "switch.sonoff_100139392c",
  "switch.sonoff_1001393418",
  "switch.sonoff_1001392bce",
  "switch.sonoff_1001392bd9",
  "switch.sonoff_100139390d"
]

Does this mean I that this code shouldwork:

action:
  - service: homeassistant.update_entity
    target:
      entity_id: {{ integration_entities('sonoff') }}

When the Jinja template is on the same line as the YAML option, you have to wrap the template in quotes like this:

action:
  - service: homeassistant.update_entity
    target:
      entity_id: "{{ integration_entities('sonoff') }}"

You don’t wrap it quotes when it’s on a separate line like this (multi-line):

action:
  - service: homeassistant.update_entity
    target:
      entity_id: >
        {{ integration_entities('sonoff') }}

If you only want the sensors whose entity_id contains the word temperature, you can filter the list like this:

action:
  - service: homeassistant.update_entity
    target:
      entity_id: >
        {{ integration_entities('sonoff')
          | select('search', 'temperature') | list }}

If you want sensors containing either ‘temperature’ or ‘humidity’ in their entity_id then use this:

action:
  - service: homeassistant.update_entity
    target:
      entity_id: >
        {{ integration_entities('sonoff') 
          | select('search', 'temperature|humidity') | list }}
2 Likes

This works perfectly and even more importantly is a super well-taught lesson!

Thank you all!

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.

2 Likes