Adding sonoff pow energy sensor values to switch entity as attributes

I’ve recently gotten a sonoff pow v2 working with sonoff-tasmota flashed to it.
configuring the mqtt switch and mqtt sensor entities in configuration.yaml and getting them integrated into the frontend was easy enough, but i can’t find a way to assign the sensor data as attributes of the switch itself.

i would like to be able to see the power attributes in the more info dialog that comes up when you click the switch title. the etekcity voltson (using vesync) does this by default:

i’ve seen a few threads that say using appDaemon is a way to achieve this, but what i’m attempting seems so simple that i would think there would be a way to accomplish it in yaml that i’m just unaware of. i’ve also looked at template switches and mqtt switches but don’t see any obvious avenues to adding attributes to them in their configuration.

alternatively, i’ve considered trying to modify the tasmota mqtt code flashed to the device via the arduino ide, in hopes that maybe i could structure the topics in such a way that hassio would automatically associate them together and merge the sensor data and switch into one entity. however, as far as i can tell, there isn’t any implementation in hassio to perform this.

is there a simple solution to adding the power usage telemetry to a switch as attributes in hassio?
…or at least a well-documented complex solution?

Hi, here is how i added in all the values for my Sonoff POW, hope it helps

#sensor sonoff_pow_watts:
  - platform: mqtt
    name: Industrial Fan Current Power Usage
    state_topic: "tele/sonoffpow/SENSOR"
    value_template: "{{value_json['ENERGY'].Power }}"
    qos: 1
    unit_of_measurement : "W"

#sensor sonoff_pow_volts:
  - platform: mqtt
    name: Industrial Fan Current Voltage
    state_topic: "tele/sonoffpow/SENSOR"
    value_template: "{{ value_json['ENERGY'].Voltage }}"
    qos: 1
    unit_of_measurement : "V"

#sensor sonoff_pow_current:
  - platform: mqtt
    name: Industrial Fan Current Amps
    state_topic: "tele/sonoffpow/SENSOR"
    value_template: "{{ value_json['ENERGY'].Current }}"
    qos: 1
    unit_of_measurement : "A"

#sensor sonoff_pow_kwh_today:
  - platform: mqtt
    name: Industrial Fan Todays Power
    state_topic: "tele/sonoffpow/SENSOR"
    value_template: "{{ value_json['ENERGY'].Today }}"
    qos: 1
    unit_of_measurement : "kWh"

#sensor sonoff_pow_kwh_yesterday:
  - platform: mqtt
    name: Industrial Fan Yesterdays Power
    state_topic: "tele/sonoffpow/SENSOR"
    value_template: "{{ value_json['ENERGY'].Yesterday }}"
    qos: 1
    unit_of_measurement : "kWh"

#sensor sonoff_pow_kwh_total:
  - platform: mqtt
    name: Industrial Fan Total Power
    state_topic: "tele/sonoffpow/SENSOR"
    value_template: "{{ value_json['ENERGY'].Total }}"
    qos: 1
    unit_of_measurement : "kWh"

thanks, but my issue isn’t how to implement the sensor into hass itself. in fact, i’m using that exact code that you provided. what i’m trying to do is find a way to assign the output values of those sensors to the sonoff switch entity as attributes, as shown in my screenshot above.

Any update about this ? have you found a solution ?

If you want to alter the switch component to add attributes to it you will have to edit the component or make a custom component.

Do you find a solution? I‘m having the same issue

1 Like

looking for this too

Literally spelled out in the docs. I’ll take OP’s config and show it:

  - platform: mqtt
    name: Industrial Fan
    state_topic: "tele/sonoffpow/SENSOR"
    value_template: "{{value_json['ENERGY'].Power }}"
    qos: 1
    json_attributes_topic: "tele/sonoffpow/SENSOR"
    json_attributes_template: "{{ value_json['ENERGY'] | tojson }}"

This says how to add info to a sensor. Not to add info to a switch.

My mistake, let me use the other documentation to do this.

  - platform: mqtt
    name: "Bedroom Switch"
    state_topic: "home/bedroom/switch1"
    value_template: "{{ whatever you want here }}"
    qos: 1
    json_attributes_topic: "tele/sonoffpow/SENSOR"
    json_attributes_template: "{{ value_json['ENERGY'] | tojson }}"


If I test this I receive error in check config because command_topic is required (!)

Invalid config for [switch.mqtt]: required key not provided @ data[‘command_topic’]. Got None. (See ?, line ?). Please check the docs at MQTT - Home Assistant

The strange thing is that looking at the docs the topic should be not mandatory:



The MQTT topic to publish commands to change the switch state.

Here’s the code:

  - platform: mqtt
    name: Microonde v2
    state_topic: "sonoffp4/tele/SENSOR"
   # value_template: "{{value_json['ENERGY'].Power }}"
    qos: 1
    json_attributes_topic: "sonoffp4/tele/SENSOR"
    json_attributes_template: "{{ value_json['ENERGY'] | tojson }}"

I commented value_template as I don’t understand what to put in there.

Moreover please that I switched topic/prefix because I normally use setoption 19 on tasmota (enabling HA autodiscovery)

What switch do you want this to be paired onto and what platform is it?

I want to create as topic a switch for a sonoff pow r2 that has energy as attributes in order to allow an automation to switch off only if the value are above a certain power.

I don’t understand when you ask about pairing or platform. My sonoff pow is running tasmota 7.1 at the moment.

If you are wondering why I do not simply use info from the sensor the answer is that this macro control all device in a input list and if the overall power consumption of my apartment is above a value it starts to switch off one by one devices in that list. That’s why I think I need that those info are inside the swich, so I can add a condition in it:

  - service: homeassistant.turn_off
      entity_id: >-
        {%- macro dss_spegni_primo_carico() -%}
        {%- set entity_carico_da_spegnere = states.input_select.lista_carichi.attributes.options|join(',')-%}
        {%- for item in entity_carico_da_spegnere.split(',') if (states(item) != 'off' and (item != ''))-%}
        {{ item }}{% if not loop.last %},{% endif %}
        {%- endfor -%}
        {%- endmacro -%}  
        {{ dss_spegni_primo_carico().split(',') | first }}

Ok, so it uses MQTT. What is the configuration for your existing switch? All you need to do is add these to that configuration with nothing changed:

    json_attributes_topic: "sonoffp4/tele/SENSOR"
    json_attributes_template: "{{ value_json['ENERGY'] | tojson }}"

If you’re using discovery, you’ll need to turn that off and manually add all your switches and configure them properly.

Ops. Ok I will disable autodiscovery for those switches and see if it works. Thanks!

Just for the record:

I send this commands via console:

SetOption19 0 (remove autodiscovery)

FullTopic 1 (restore default topic).

Then added code as you suggested. I added more lines found on tasmota wiki, just to be sure. Don’t know if needed:

  - platform: mqtt
    name: "Asciugatrice"
    state_topic: "stat/SonoffP1/RESULT"  
    value_template: "{{ value_json.POWER }}"
    command_topic: "cmnd/SonoffP1/POWER"
    json_attributes_topic: "tele/SonoffP1/SENSOR"
    json_attributes_template: "{{ value_json['ENERGY'] | tojson }}"
    payload_on: "ON"
    payload_off: "OFF"
    availability_topic: "tele/SonoffP1/LWT"
    payload_available: "Online"
    payload_not_available: "Offline"
    qos: 1
    retain: false

Now I have a switch with all info from energy available.

It’s time to find a way to change the macro!

Thank you!

The macro you posted?

try this (it can be optimized but i’m splitting it up so you understand better

{% set attribute = 'power' %}
{% set value = 5 %}
{% set entities = states | selectattr('entity_id','in', states.input_select.lista_carichi.attributes.options) | list %}
{% set on_entities = entities | selectattr('state','eq','on') | list %}
{% set on_and_above_value = on_entities | selectattr('attributes.%s'%attribute, '>', value) | list %}
{{ on_and_above_value | first }}

Ciao Petro,

I waited before respond to be sure : it seems not to work.

It does not turn on or off anything.

Any hint?

I’d have to see what you made