Im trying to create two template sensors that report the state of the washing machine and tumble dryer from ESP home devices reporting the current Amps of each devices
Both sensors report Off even though both are reporting over 0.2 A. I suspect this is because I’m using float and the reported value is probably text (as in ‘5.96 A’ where A is amps). I have no clue how to fix
{% set value = state_attr('sensor.washing_machine_amperage', 'A') %}
{% if ' ' in value %}
{% set value = value.split(' ')[0] | float %}
{% if value < 0.1 %}
off
{% else %}
running
{% endif %}
{% else %}
failed
{% endif %}
as a sidebar, I recommend using a binary sensor instead of your normal sensor:
binary_sensor:
- platform: template
sensors:
washing_machine:
friendly_name: Washing Machine
device_class: power
value_template: >
{% set value = state_attr('sensor.washing_machine_amperage', 'A') %}
{% if ' ' in value %}
{% set value = value.split(' ')[0] | float %}
{% if value < 0.1 %}
off
{% else %}
on
{% endif %}
{% else %}
unavailable
{% endif %}
Then you can add a device_class to it and the icon will change based on it’s state. If you use state_color, it will also change color in the UI when on. Lastly, automations will be easy to trigger off of.
trigger:
- platform: state
entity_id: binary_sensor.washing_machine
to: 'on'
Your original template is using state_attr in which you provided the entity_id and the value of the unit_of_measurement attribute. You should just be getting the state. The proper way to get an attribute would be