I have 2 Sonoff S31 switches that are running Sonoff-Tasmota 6.4.1. I’ve added them to Home Assistant using the auto-discovery mechanism and I can see both of them just fine.
I would like to know if there’s a way or any examples on getting the combined sensor values (total power, total energy) of both or all tasmota switches either through groups and templating, MQTT wild carding, or even as a last resort, peeking into the database file and grabbing whatever I need.
I thought about using a group to initially define all of my switches. Something like this:
sonoff_switches:
name: Sonoff Switches
entities:
- switch.sonoff_switch1
- switch.sonoff_switch2
# Trying to dynamically build sensors based on the above defined list of entities.
# eg:
# - sensor.sonoff_switch1_energy_power
# - sensor.sonoff_switch2_energy_power
sonoff_energy_sensors:
name: Sonoff Energy Monitors
entities:
{% for sensor in states.group.sonoff_energy_switches.attributes.entity_id %}
- {{ sensor }} # somehow split on . delimiter and append _energy_power here
{%- endfor -%}
But I don’t think templates work in groups because I get errors when checking config.
I also thought about wildcard MQTT subscriptions, but I haven’t seen examples if its possible to iterate through the value_jsons if we get multiple messages back. Can you even address value_json like this?
- platform: mqtt
name: "Total power"
state_topic: "+/tele/SENSOR"
value_template: "{{value_json[0]['ENERGY']['POWER'] }}"
My last option I can think of is to use something like:
sensor:
- platform: sql
db_url: !secret recorder_db_url
queries:
- name: Total Power (W)
query: >
SELECT sum(state)
FROM (
SELECT state_id, entity_id, state
FROM states
WHERE domain = 'sensor' AND entity_id LIKE '%energy_power' AND state != 'unknown' AND state != 'unavailable'
GROUP BY entity_id
ORDER BY state_id DESC )
to pull the last states of all my energy sensors and add them up. But how often does would a sql sensor update and would it cause a lot of CPU usage to run a nested query like this over and over?
In the future, I plan to add more Sonoff S31s running Tasmota around the house and I would like to be able to get the combined usage of all the sensors without manually adding them to a sensors configuration each time.
Any ideas/suggestions?