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.
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:
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:
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.
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?