Template Sensor Problem Using Amps

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

I’ve wrote this…

      washing_machine_state:
        friendly_name: "Washing Machine State"
        value_template: >-
          {% if state_attr('sensor.washing_machine_amperage', 'A')|float < 0.1 %}
            off
          {% elif state_attr('sensor.washing_machine_amperage', 'A')|float > 0.2 %}
            Running
          {% else %}
            failed
          {% endif %}

      tumble_dryer_state:
        friendly_name: "Tumble Dryer State"
        value_template: >-
          {% if state_attr('sensor.tumble_dryer_amperage', 'A')|float < 0.1 %}
            off
          {% elif state_attr('sensor.tumble_dryer_amperage', 'A')|float > 0.2 %}
            Running
          {% else %}
            failed
          {% endif %}

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

Thanks

if the state is <number> A

{% 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'

Firstly thank you for your help.

I’ve taken your advice and moved to your binary sensor, all you’ve said makes sense.

however Ive put your code into config.yaml and I now just get a Washing Machine unavailable icon

Can you post a screenshot of ‘sensor.washing_machine_amperage’ in the dev tools -> states page?

certainly can…

So the ‘A’ is superfluous, that’s just unit of measure, ignore it

binary_sensor:
- platform: template
  sensors:
    washing_machine_running:
      friendly_name: Washing Machine Running
      device_class: power
      value_template: >
        {% set value = states('sensor.washing_machine_amperage') | float %}
        {{ value >= 0.2 }}

Edit:

No, your syntax was wrong

In which case surely my original code would have worked?

I’ll try your code now, thanks

Ok, you’re using the wrong syntax and misunderstanding what an attribute is.

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

{{ state_attr('sensor.washing_machine_amperage', 'unit_of_measurement') }}

And it would return A.

The proper way to get the state is

{{ states('sensor.washing_machine_amperage') }}

Also, if you don’t want startup ‘oddness’, add an availability template.

binary_sensor:
- platform: template
  sensors:
    washing_machine:
      friendly_name: Washing Machine
      device_class: power
      value_template: >
        {% if states('sensor.washing_machine_amperage') | float < 0.1 %}
          off
        {% else %}
          on
        {% endif %}
      availability_template: >
        {{ states('sensor.washing_machine_amperage') != 'unavailable' }}

Ok, I sort of understand, Mutt’s codes worked fine, I will amend to add your code into it and do a little more reading.

Thank you both for your help :slight_smile: