Template binary sensor logic

I’m having trouble making a template binary sensor that’s the OR of two other sensors. The individual sensors are working correctly. I’ve tried changing OR to AND and using the same sensor in both is_state() to force both arguments to be the same. I expected that to force it to be the same state as the main sensor, but basement_occupancy still never changed state.

I’m new to this, so there’s likely some subtle syntax issue that I’m missing.

binary_sensor:
  - platform: template
    sensors:
      basement_occupancy:
        entity_id:
          - sensor.basement_sensor_burglar_19_10
          - binary_sensor.basement_stairs_motion
        value_template: >-
          {%- if is_state("sensor.basement_sensor_burglar_19_10", "on")
                or is_state("binary_sensor.basement_stairs_motion", "on") -%}
            on
          {%- else -%}
            off
          {%- endif %}
        sensor_class: occupancy

Use the template dev tool to debug. It’ll let you test the value_template till you’ve got the right output.

You can also do things like show you the current values that you’re working with. Try pasting this into the template tool:

basement burglar: {{ states.sensor.basement_sensor_burglar_19_10.state }}
basement stairs: {{ states.binary_sensor.basement_stairs_motion.state }}

my code:
{%- if is_state(“sensor.basement_sensor_burglar_19_10”, “on”) or is_state(“binary_sensor.basement_stairs_motion”, “on”) -%}on
{%- else -%} off
{%- endif %}

I am seeing similar behavior

binary_sensor:
  - platform: template
    sensors:
      device_tracker:
        value_template:  >-
          {%- if is_state("device_tracker.myphone", "home")
              or is_state("device_tracker.iphone", "home")
              or is_state("device_tracker.her_iphone", "home") -%}
          off
          {%- else -%}
          on
          {%- endif -%}

If I test it in the developer tool, I get the changed status, but when I look at the value in the ‘Current Entities’ section, it never changes.

1 Like

You need an entity_id (optionally, a list of entity_ids) to trigger the change. The changing state of the entity_id is what causes the template to be re-evaluated.

entity_id:
  - device_tracker.myphone
  - device_tracker.iphone
  - device_tracker.her_iphone

Hmmm, I added that, no errors, but it still refuses to update when the none of the device trackers are home.

Did you change a device from home to away and back? I’m pretty sure that it’s a change in the state of the device that’ll trigger things.

Yes. I’m using the bluetooth based presence, so it is easy to test. Is there another way to define an automation based off of a logical OR? My goal is to turn of the Christmas Tree lights if no one is home.

Yeah. Definitely. You can use the state of group.all_devices as a trigger for an automationq. It’s home when anyone is home. Away when everyone is away.

Or use a list of specific devices as your entity_id for a state trigger.

There’s no need to use a sensor.

2 Likes

Thanks! I will try that as soon as I can get back in front of my system.

Worked like a charm!

This also worked for me

    value_template: >-
      {{  is_state('sensor.basement_sensor_burglar_19_10', 'on') or 
          is_state('binary_sensor.basement_stairs_motion', 'on') }}