Template works in template editor but not as trigger for an automation

Hi, I’m new to templates so I tried my template first in Dev tools, then used it as trigger for an automation.

In an automation I want to trigger turning on the light when at the same time the Xiaomi Aqara sensor detects motion, and the light reading from the same sensor is lower than a threshold.

I am using a template because a condition would not work: the same automation also turns off the light when no motion is detected (I use two triggers and a Trigger condition).

So, after cleaning up the “turn off” trigger, which works as intended, the trigger not triggering is:

alias: 'Presence: Lights green rooom'
trigger:
  - platform: template
    value_template: >-
      {{ states('input_boolean.presence_green_room') and
      (states('sensor.0x00158d0007504da8_illuminance_lux') | float < 150) }}
    id: present
condition: []
action:
...
mode: single

After walking into the room and verifying that the automation was not triggered (there’s no trace log at all), that the input_boolean was “on”, and that the brightness was lower than 150, I checked the template in the dev tools:

{{ states('input_boolean.presence_green_room') and
        (states('sensor.0x00158d0007504da8_illuminance_lux') | float < 150) }}

The output is “true”.

So why wasn’t the automation triggered?

Thanks in advance.

Just for what it’s worth, you actually could use conditions. You simply have the motion detector as the trigger, with no to or from state - so that it will trigger the automation whenever to changes to any state.

The in actions you use choose, which now enables you to choose actions based on the state of the motion sensor, and then you can add conditions.

I don’t see anything obviously wrong with your template though - well except from the fact that if the input_boolean was already on, then no state has changed and thus the automation won’t be triggered…

{{ states('input_boolean.presence_green_room')  }}  Returns the current state.

Maybe you should try

{{ is_state('input_boolean.presence_green_room','on') and
states('sensor.0x00158d0007504da8_illuminance_lux') | float < 150 }}

A good way of getting the syntax right, but not a guarantee it’s going to work. I tried your template in my own Dev Tools and the output was also “true”. But I don’t have a input_boolean.presence_green_room in my system… :laughing:

I will, but the documentation explains that “on” is regarded as “true”:

I would have been my fallback position if no other solution is found.

I checked the history, the mistake is not in the input values, it’s in the template.

Thanks, I didn’t know this.

Template triggers only fire when the template changes from false to true, it will not trigger if it’s already true.

And they only fire once. To fire a second time the value has to change back to false, then to true again.

I know, that’s why I verified from the GUI that when I entered the room the input_boolean changed state, and since the template has an “and” condition, the template as well would turn from false to true (the measured brightness was already below the threshold).

That’s only if the whole template returns, e.g., “on”.

Try in the template debugger:

{{ states("input_boolean.presence_green_room") and true }}

You’ll see that whatever the input_boolean value is, true will be returned.
That’s because, in this context, if states() return anything, it will be considered true.

1 Like

It worked, that was the issue.

I will nevertheless try the other suggestion to trigger on bare state change and work out the rest in the actions.