Template binary sensor question

Hi Everyone,

What does the >- do in front of value_template in this scenario?

value_template: >-
   {{ (states('sensor.hall_lux')|float < 5 )
        or states('sensor.study_lux')|float < 35 }}

I believe it indicates that multiple lines follow after it.
You can remove it, but you’d have to put your entire template on one line and surround it with quotes.

The > indicates the template covers more than one line and strips out new line characters inside the template, the - strips the new line character(s) at the end of the template.

Have a play with the interactive demonstration here: https://yaml-multiline.info/

Thank you i’ll have a play

Also whilst we are on the subject how would I put > or equal to?

See this reference: https://jinja.palletsprojects.com/en/master/templates/#math

Sorry for all the questions, I’ve been using HA for about 18 months and have always been able to find the answers without posting but this time I’m struggling.
Here is my binary sensor:

  - platform: template
    sensors:
      house_lux:
        friendly_name: "House Lux"
        delay_on:
          minutes: 15
        value_template: >-
          {{ (states('sensor.hall_lux')|float < 5 )
              or states('sensor.study_lux')|float < 35 }}

Is it possible to get the binary sensor to ignore an unavailable entity (eg if sensor.hall_lux goes down)
Currently if a sensor goes unavailable the binary sensors switches on.

Jesus that was easy!! Thanks

Use the availability template:

availability_template: "{{ states('sensor.hall_lux') not in [ 'Unavailable', 'None', 'Unknown' ] and states('sensor.study_lux') not in [ 'Unavailable', 'None', 'Unknown' ] }}"

Hi Tom,

That worked but it wasn’t quite what I’m after. Can I get the binary sensor to ignore an unavailable sensor and base its calculations on the other sensor?

I’ve done it. I just needed the shove in the right direction:

value_template: >-
          {{ (states('sensor.study_lux')|float < 35 )
              and (states('sensor.study_lux')|float >= 0 )
              and (states('sensor.study_lux') not in [ 'unavailable', 'none', 'unknown' ])
              or (states('sensor.hall_lux')|float < 5 )
              and (states('sensor.hall_lux')|float >= 0 ) 
              and (states('sensor.hall_lux') not in [ 'unavailable', 'none', 'unknown' ]) }}

Thanks a lot, i really appreciate the help

Tom, Just be playing some more and I’ve found a better way using your availability_template.

        value_template: >-
          {{ (states('sensor.study_lux')|float < 35 )
              and (states('sensor.study_lux') not in [ 'Unavailable', 'None', 'Unknown' ])
              or (states('sensor.hall_lux')|float < 5 )
              and (states('sensor.hall_lux') not in [ 'Unavailable', 'None', 'Unknown' ]) }}
        availability_template: >-
          {{ (states('sensor.hall_lux') not in [ 'Unavailable', 'None', 'Unknown' ] )
              or (states('sensor.study_lux') not in [ 'Unavailable', 'None', 'Unknown' ]) }}
# availibility template notes:
# must be 'or' be unavailable when both entites unavailable
# must be 'and' to be unavailable when either entity is unavailable

Thanks again :pray:

2 Likes

No worries I think this might be back to front though.

 # must be 'or' be unavailable when both entites unavailable
 # must be 'and' to be unavailable when either entity is unavailable

If you use and both sensors must be available. If you use or only one has to be available (which is what you want for your last version of the value_template).

It’s correct, counter intuitive i know. Try it and see!!!

I did. It supports my version:

I’ve just tried it again as I thought I was going mad:

But it’s the same as before:
If I used ‘and’ it puts the binary sensor to unavailable when only one of the sensors is unavailable
If I use ‘or’ it puts the binary sensor unavailable when both of the sensors is unavailable.



This is the code:

# House Lux Sensor (with 15 Min Delay)
  - platform: template
    sensors:
      house_lux:
        friendly_name: "House Lux AND"
#        delay_on:
#          minutes: 15
        value_template: >-
          {{ (states('sensor.study_lux')|float < 35 )
              and (states('sensor.study_lux') not in [ 'Unavailable', 'None', 'Unknown' ])
              or (states('sensor.hall_lux')|float < 5 )
              and (states('sensor.hall_lux') not in [ 'Unavailable', 'None', 'Unknown' ]) }}
        availability_template: >-
          {{ (states('sensor.hall_lux') not in [ 'Unavailable', 'None', 'Unknown' ] )
              and (states('sensor.study_lux') not in [ 'Unavailable', 'None', 'Unknown' ]) }}
              
# House Lux Sensor Immediate
  - platform: template
    sensors:
      house_lux_immediate:
        friendly_name: "House Lux OR"              
        value_template: >-
          {{ (states('sensor.study_lux')|float < 35 )
              and (states('sensor.study_lux') not in [ 'Unavailable', 'None', 'Unknown' ])
              or (states('sensor.hall_lux')|float < 5 )
              and (states('sensor.hall_lux') not in [ 'Unavailable', 'None', 'Unknown' ]) }}
        availability_template: >-
          {{ (states('sensor.hall_lux') not in [ 'Unavailable', 'None', 'Unknown' ] )
              or (states('sensor.study_lux') not in [ 'Unavailable', 'None', 'Unknown' ]) }}
# availibility template notes:
# must be 'or' be unavailable when both entites unavailable
# must be 'and' to be unavailable when either entity is unavailable

Yeah sorry. We are saying the same thing. Except you are talking about the sate unavailable, I was talking about the sate available.

Ah i see it now.
Thanks again

Has something change in HA coding as a delay of 24 hours on a binary template sensor does not work anymore? (23 hours does)