Automation Help - including custom sensor with templated state

I created an automation that was working just fine and since I added a condition to it that includes a sensor with a templated state, it has stopped working.

My automation triggers when my garage door opens at night and the house is dark. It simply turns on all my lights so I don’t stumble into a pitch black house. It worked fine when the only conditions I had was a light sensor was below a specific value and the sun was below the horizon. Everything worked as expected. However, I ran into some situations where I would leave early, before sunrise, which would leave the lights on all day, or I would come in really late from a trip and when I got home, all the lights would come on waking my kids up. The solution I came up with was to use the device tracking and if someone was home, do not trigger the automation.

So, I browsed around and found an example that monitors multiple device trackers and updates a sensor.

- platform: template
  sensors:
    someone_home:
      friendly_name: Someone Home
      icon_template: >-
        {% if is_state('binary_sensor.someone_home','on') %}
          mdi:home-account
        {% else %}
          mdi:home-outline
        {% endif %}
      value_template: "{{ is_state('person.chad','home') or  is_state('person.tiffany','home') }}"

The sensor works just fine. It returns a ‘True’ or ‘False’ if someone is home.

Here’s my automation with the new sensor included as a condition:

- id: '1559016728404'
  alias: Nighttime Dark House
  trigger:
  - entity_id: binary_sensor.ecolink_door_window_sensor_sensor
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - below: '5'
    condition: numeric_state
    entity_id: sensor.vision_security_zp3111_multisensor_4in1_luminance
  - condition: state
    entity_id: sun.sun
    state: below_horizon
  - condition: state
    entity_id: sensor.someone_home
    state: 'False'
  action:
  - data:
      entity_id: group.home_all_lights
    service: homeassistant.turn_on

I don’t see anything glaringly obvious so the only thing I can think is that I’m using the wrong value for my new sensor. Instead of True and False, should it be ‘off’ or ‘on’ on maybe even 1 or 0? If I remove the sensor condition, it goes back to working correctly.

Can anyone see where my mistake is?

I’m not sure why it happens but I’ve seen other examples of this happening. The way that i resolved it there was to force the template to return ‘true’/‘false’ like this:

value_template: >
  {% if is_state('person.chad','home') or  is_state('person.tiffany','home') %}
    true
  {% else %}
    false
  {% endif %}

then make sure you use ‘true’ or ‘false’ in your condition state.

you could try it and see if it works.

That seems to have done the trick! No more stubbing my toes on kids toys in the dark.

Thanks!

1 Like

(My poor reading skills! I’ve removed my original, redundant suggestion.)

NOTE:
Something is amiss here. Have you defined a Template Sensor or a Template Binary Sensor?

In the configuration of the icon_template, you refer to binary_sensor.someone_home. If that’s self-referential then you have created a Template Binary Sensor. If it’s not self-referential then is this a typo or a reference to another sensor with an identical name?

If you are creating a Template Binary Sensor then this is incorrect:

  - condition: state
    entity_id: sensor.someone_home
    state: 'False'

For a Template Binary Sensor, the entity_id should refer to a binary_sensor and the state should test for off.

  - condition: state
    entity_id: binary_sensor.someone_home
    state: 'off'
1 Like