Template unexpected output

New at coding and HA, so please bare with me.

First to explain what I am trying to do:
I have a KASA power monitoring smart plug that I have connected my AC to. I have learned that it’s not a good idea to abruptly turn off the power while the compressor is still working, and best to turn the plug off when the compressor is off, in another words when power draw is 0 amps. Therefore I want to have a trigger, a helper button most likely to, and then having a wait templete waiting until the amp draw is 0, then executing the next executing the next action to turn off the plug.

The issue:
I run the following code in developer tools template and always get “False”, even though the sensor reads 0.00:

{{ state_attr('sensor.master_bedroom_ac_current', 'masterac') == 0.00}}

When I just try to just check the value it just returns “None”:

{{ state_attr('sensor.master_bedroom_ac_current', 'masterac') }}

Please help, what am I doing wrong?

Thank you!

  1. Ensure you have the correct spelling of the sensor’s entity_id and the attribute’s name. If you have misspelled either one, the state_attr() function will report none.

  2. Go to Developer Tools → States

  3. Find sensor.master_bedroom_ac_current in the list

  4. Look in the Attributes column and find masterac

  5. What is the value shown there?

1 Like

Thank you for the quick reply.

Here is what I see in Developer Tools → States:

I tried all attributes listed, but none of them show the 0.0 value.

The screenshot confirms there is no attribute named masterac.

What you should be using is the states() function to get the sensor’s state value.

{{ states('sensor.master_bedroom_ac_current') | int(0) == 0 }}

Every entity’s state value is a string (i.e. text). Even if it looks like a number, it’s still handled as a string.

In order to compare one number to another, you must ensure both are truly numbers; you can’t compare a string to a number. That’s why the template uses the int() filter to convert the string to an integer before it compares the result to 0.

The 0 in int(0) is the default value. If int is unable to convert the sensor’s state value to an integer, it will report whatever is set as the default value (in this example the default value has been set to 0).

1 Like

Learned something new today, thank you for your time and patience!

1 Like