Trouble tracking voltage in history from Sonoff S31 wifi switch

I have a Sonoff S31 wifi switch, and I would like to track the voltage over time from its energy monitoring feature.

I have it connected to home assistant via eWeLink:

I can see the switch as an entity that includes the voltage:

However, this value does not appear in the history, only the switch state does:
image

Would anyone know how to extract this value from the entity and log it in the history?

Thank You

You will have to create a template sensor to extract the voltage attribute. e.g.

# configuration.yaml

template:
  - sensor:
      - name: Voltage of something
          - device_class: voltage
          - state_class: measurement 
          - unit_of_measurement: V
          - state: "{{ state_attr('switch.your_switch_entity_here','voltage') }}"

This is only an example. You will need to supply a sensible name, the actual entity id and the correct attribute letter case for the attribute (capital or lower case letters). The last two can be found in Developer Tools → States.

1 Like

I appreciate the help.

I played with the syntax until I got it to accept this:

template:
  - sensor:
      - name: House Voltage
        state: "{{ state_attr('switch.10014f79eb','voltage') }}"

This will give me the voltage, but as a string instead of a number:
image

I tried adding the rest of the fields like this: (i got a bad syntax warning when leaving the hyphens in)

template:
  - sensor:
      - name: House Voltage
        device_class: voltage
        state_class: measurement 
        unit_of_measurement: V
        state: "{{ state_attr('switch.10014f79eb','voltage') }}"

But the item disappears from my entities, I assume because the config is invalid.

I also tried only adding back the state_class part with the same result.

I also tried just trying to convert the type of the state with this:

state: "{{ state_attr('switch.10014f79eb','voltage') }| float % }"

and this:

state: "{{ state_attr('switch.10014f79eb','voltage') | float %  }}"

with the same result of it no longer appearing in the list of entities.

Any thoughts on where I’m going wrong? I’m not sure if it’s a syntax issue, data type issue, both, or neither.

Are you entering this in

Or are you attempting to use a UI Helper?

This is correct for configuration.yaml:

template:
  - sensor:
      - name: House Voltage
        device_class: voltage
        state_class: measurement 
        unit_of_measurement: V
        state: "{{ state_attr('switch.10014f79eb','voltage') }}"

If you are creating a UI template helper just enter this in the template field:

{{ state_attr('switch.10014f79eb','voltage') }}

Everything else is set using the other options in the UI. Including the unit, which is what is required for it to be graphed as a number.

1 Like

I am working in configuration.yaml.

I tried using the UI, but got stuck:

I can’t find the right device from the drop down, but maybe that isn’t needed.

The error text implies I’m getting the string ‘120.86 V’. Do I need to manually remove the ‘V’ from the string and convert to a float?

If I paste

template:
  - sensor:
      - name: House Voltage
        device_class: voltage
        state_class: measurement 
        unit_of_measurement: V
        state: "{{ state_attr('switch.10014f79eb','voltage') }}"

into my configuration.yaml, the entity will disappear again. Could this be caused by a failed attempt to convert it to a numeric value?

I’m not sure if this is the most elegant way to do it, but I removed the V in the string using this:

{{ state_attr('switch.10014f79eb','voltage')|replace('V','') }}

And it seems that allows the type conversion to succeed, and it now appears as a numeric value.

Yes that’s the way to do it.