My binary sensor template is always OFF

I want to show if the laundry machine has finished, or if it is still running. So I put an monitored power outlet for the laundry, and this seems to work fine.
My dashboard displays all the various energy/power etc consumptions nicely.

So, to make the display a bit more Yes/No friendly, I decided to create a binary sensor for this.

Found this excellent example, Template - Home Assistant which I implemented.

template:
  - binary_sensor:
      - name: "Washing Machine"
        delay_off:
          minutes: 5
        state: >
          "{{ states('sensor.smart_plug_2210195428482851070348e1e9ac2279_power')|float(0) > 0}}"

But this sensor is always showing Off (which I presume is false).

I try it in the template editor, and the state there shows state:True

I change the code in the template, to be <= 0 (to trigger something else), and yes the template editor (when I tried in it) shows state:False now.
But the dashbord, as well as the binary sensor I created still shows Off (false)

I have no clue where I am doing something wrong.
Don’t have the most experience with HA, just started playing with templates and more integrations here.

Found a similar questions when I checked

Below comes a few screenshots that hopefully will illustrate the issue and my configuration a bit more.

What my dashboard shows
Screenshot from 2023-09-28 14-44-31

What Developers tools - States shows

And my Developers tools - Template

And the binary sensor template it self.
Don’t know how to expand the template code, sorry

And the entity I check for power consumption
Screenshot from 2023-09-28 15-04-42

And lastly, some version information
Screenshot from 2023-09-28 14-46-42

Dont quote multi-line templates. Do this:

        state: "{{ states('sensor.smart_plug_2210195428482851070348e1e9ac2279_power')|float(0) > 0}}"

Or this:

        state: >
          {{ states('sensor.smart_plug_2210195428482851070348e1e9ac2279_power')|float(0) > 0}}
1 Like

Thanks Tom
I changed to just a one liner

template:
  - binary_sensor:
      - name: "Washing Machine"
        delay_off:
          minutes: 5
        state: "{{ states('sensor.smart_plug_2210195428482851070348e1e9ac2279_power')|float(0) > 0}}"

But still same issue
image

remove the quotes.

"{{ states('sensor.smart_plug_2210195428482851070348e1e9ac2279_power')|float(0) > 0}}"

evaluates to “True” or “False” strings not boolean and therefore will be always Off.

Thanks for the tip ClassicGOD

I changed to

template:
  - binary_sensor:
      - name: "Washing Machine"
        delay_off:
          minutes: 5
        state: {{ states('sensor.smart_plug_2210195428482851070348e1e9ac2279_power')|float(0) > 0}}

But no change
still indicating Off, while the Developer tool Template tester indicates True, and the power is > 0

Yeah don’t do that. You only remove the quotes for multi-line templates.

Put this in Developer Tools → Templates and post the result:

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

Hi
As per request

template:
  - binary_sensor:
      - name: "Washing Machine"
        delay_off:
          minutes: 5
        state: {{ states('sensor.smart_plug_2210195428482851070348e1e9ac2279_power') }}

Result

template:
  - binary_sensor:
      - name: "Washing Machine"
        delay_off:
          minutes: 5
        state: 2.324

And this

template:
  - binary_sensor:
      - name: "Washing Machine"
        delay_off:
          minutes: 5
        state: {{ states('sensor.smart_plug_2210195428482851070348e1e9ac2279_power')|float>0 }}

gives

template:
  - binary_sensor:
      - name: "Washing Machine"
        delay_off:
          minutes: 5
        state: True

The complete code that you’re posting is for use when editing YAML in configuration.yaml and included files.

If you’re using the UI to create the sensor, just paste this into the “State template” box:

{{ states('sensor.smart_plug_2210195428482851070348e1e9ac2279_power')|float(0)>0 }}

Note that this will be on whenever the smart plug reports any positive value. You may want to compare with e.g. 1W rather than 0W.

:man_facepalming:
Jeez…

Thank you very very much :slight_smile: I have been so confused on this one… and could not figure out what I did wrong.

But if I want to use the delay, I need to modify the configuration.yaml file manually I take it.

Yeah, I think you would have got a helpful response quicker had the ability to create template sensors from the UI not been so new — us “experts” aren’t used to it, as most of us edit YAML rather than use the UI.

Yes. When you put that code in (with @tom_l’s fixes in the first response), make sure there’s only one top-level template: header in the file. If it has e.g. template: !include templates.yaml, put it in that file but without the first line.

Thanks again, the muddy water got very much clearer now :slight_smile:

And no issues if it takes a bit of time, just very grateful with some advice and suggestions and possible solutions :slight_smile:

Here’s a screenshot of an example in the documentation. It’s the full YAML configuration for a Template Binary Sensor. See the highlighted template following the state option?

That’s the part you would copy-paste into the State template* field when creating a Template Binary Sensor via the UI.

Thanks :slight_smile:
Now I understand how the template works both in YAML as well as in UI.