[Help} Template sensor not working

Hello

First of all thank you to all in the forums, reddit and github for contributing to this amazing project called HA. I am a complete noob and have learned so much over the past few months to get a decent smart home up and running.

The issue I am having is with my Aqara fp2 presence sensor. The entity reports states as detected or clear. I have one in my living room that has 2 zones one for the sofa, and one for the rest of the living room.

With my research I thought creating a tempalte sensor to combine both entities into one would work, but its not working. The sensor shows “clear” at all times even if presence is detected.

The automation I am trying to create is if there is no presence detected for 10 minutes, and the TV is on. To turn the TV off.

binary_sensor:
  - name: "Living Room Presence"
    state: >
      {{ is_state('binary_sensor.aqara_presence_sensor_presence_sensor_1', 'on') or
         is_state('binary_sensor.aqara_presence_sensor_presence_sensor_2', 'on') }}
    device_class: occupancy

I used somebody’s code on Reddit for a template sensor as well as ChatGPT to help me write this. It simply does not work.

Things I’ve tried:
-Aqara FP2 sensor works fine, entities report correct states
-Logs show no errors or issues
-If I run the automation, it works - since the sensor shows “clear” even if it is not

If a template sensor isnt the best way to do this, I am open to suggestions

P.S: Why does HA not allow to check current states of entities? For example, I have alot of automations in mind with the presense sensor, but the automatiosns only run when the state is CHANGED from clear TO detected or vice versa.

Is there anyway for an automation to simply check what the current state of the entity is?

Thanks again everybody.

Please describe where you added the sensor configuration in your post.

Automation Basics

Triggers are event-driven. This is done because it is more efficient, robust, and less dependent on the operating system timing compared to continuous polling.

Conditions can be used check an entity’s current state once an automation has been triggered.

Shoot! Sorry.

I did it via the UI

Settings > devices > create a helper > template > template a binary sensor

Let me know if any other information is required. Thanks!

Only the template should be in the State template field, not the YAML configuration shown in your first post.

Make sure the UI Template Helper editor looks like the following:

What you’re looking for is a trigger-based template sensor. You setup that sensor and let your automations react on that sensors state change. :slight_smile:

See here:

Something like this:

template:
  - trigger:
      - platform: state
        entity_id: binary_sensor.aqara_presence_sensor_presence_sensor_1
      - platform: state
        entity_id: binary_sensor.aqara_presence_sensor_presence_sensor_2
    binary_sensor:
      - name: "Presence detected"
        state: >
          {{ is_state('binary_sensor.aqara_presence_sensor_presence_sensor_1', 'on') or is_state('binary_sensor.aqara_presence_sensor_presence_sensor_2', 'on') }}

Now you can react in an automation with a state change trigger of this binary_sensor.

Or did I miss your point? :slight_smile:

EDIT: changed because wrong state, see the next post, thanks @Didgeridrew

A binary sensor’s actual state will never be “detected” that is just the “friendly” frontend representation given to occupancy sensors. A binary sensor’s actual state is either “on” or “off”.

1 Like

This worked. Thank you.

This is what I get for using chatGPT.

Also I totally understand why the logic for automations is based off of triggers.

Thanks so much for your replies. I am sure i will be back with more questions as i continue my HA journey

1 Like