State Condition with Template for Time

I’m trying to set up a condition that if there’s been no motion for X minutes, then the light turns off.

This works fine - however I’m wanting to use a Input Number to change on demand…

condition: state
entity_id: binary_sensor.sensenode_motion
state: 'off'
for:
  hours: 0
  minutes: 2
  seconds: 0

So something similar to this works for the trigger

condition: state
entity_id: binary_sensor.sensenode_motion
state: 'off'
for: 00:0{{ states('input_number.lights_automation_lights_off_after') |int}}:00

But them I’m receiving the following error

Message malformed: offset 00:0{{ states(‘input_number.lights_automation_lights_off_after’) |int}}:00 should be format ‘HH:MM’, ‘HH:MM:SS’ or ‘HH:MM:SS.F’ for dictionary value @ data[‘condition’][2][‘for’]

Will I have to change this into a template?

I don’t think for supports templates since it doesn’t say anything about it here. However your error isn’t about that. The error your seeing is because you didn’t wrap the value in quotes so your YAML is invalid. Try this:

for: "00:0{{ states('input_number.lights_automation_lights_off_after') |int}}:00"

And see if that works before deciding whether to switch to a template condition or not.

That doesn’t work either… and strange as I have this as my trigger…

platform: state
entity_id: light.bedroom
to: 'on'
for: 00:0{{ states('input_number.lights_automation_lights_off_after') |int}}:00
enabled: true

oh. I guess it figured out that its a string due to the leading zeroes. Yea ok it only wants a hard-coded value no template. I read “message malormed” and stopped but should’ve kept reading

Message malformed: offset 00:0{{ states(‘input_number.lights_automation_lights_off_after’) |int}}:00 should be format ‘HH:MM’, ‘HH:MM:SS’ or ‘HH:MM:SS.F’ for dictionary value @ data[‘condition’][2][‘for’]

It definitely wants a hard-coded time string there.

You can do it in the trigger:

Yeah looks like it doesn’t like template in the condition, but ok in the trigger.
Even this doesn’t work

condition: state
entity_id: binary_sensor.sensenode_motion
state: 'off'
for:
  hours: 0
  minutes: {{states('input_number.lights_automation_lights_off_after')|int}}
  seconds: 0

BlockquMessage malformed: expected float for dictionary value @ data[‘condition’][2][‘for’][‘minutes’]
ote

Guess I need a template to do it…

That really would need quotes around it if it were accepted.

Can you not flip your logic around to trigger on lack of motion? Another option would be a “no motion for a while” trigger-based binary sensor which you could check the current state of in a condition.

This is exactly how it have it now.
If no motion in X minutes and light is on then turn it off.

The issue here, if you turn the light on without motion, that will never trigger and turn the lights off.

You’re aware that conditions don’t wait right? Correct me if I’m wrong but it seems like this is the automation you’re trying to write:

trigger:
  platform: state
  entity_id: light.bedroom
  to: 'on'
  for: 00:0{{ states('input_number.lights_automation_lights_off_after') |int}}:00
  enabled: true
condition:
  condition: state
  entity_id: binary_sensor.sensenode_motion
  state: 'off'
  for: 00:0{{ states('input_number.lights_automation_lights_off_after') |int}}:00
action:
  service: light.turn_off
  data:
    entity_id: light.bedroom

If so then I don’t think this is actually what you want. What will happen in this automation is this:

  1. When the light has been on for X minutes (X is the input number), trigger
  2. If the motion sensor has been off for X minutes - turn off the lights
  3. If the motion sensor has not been off for X minutes - do nothing and stop

I’m guessing the motion sensor turning on turns on the light. Which likely means even if someone walked in and out of the room as fast as possible the motion sensor is probably going to be off a few seconds less then the light is going to be on. Which means this automation will do nothing at all, it will just fail the condition and stop every time.

Let me know if I’m off-base here. If so please share your automation so we can better understand what you’re trying to do. condition trips people up a lot because people think the automation will wait until it is true to proceed and that’s not how it works. It seems like you might be thinking along those lines from what you’ve shared.

This is pretty much what is happening yes.
I have a sensor that turns the light on when conditions are met.

I also have this to turn off the lights, but this only triggers when the motion is for for X (where x= 1 minute)

- id: '199999999'
  alias: '[Light Auto Off With No Motion] [Office] Off after X minutes without motion'
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.node
    to: 'off'
    for: 00:0{{ states('input_number.lights_automation_lights_off_after') |int}}:00
  condition:
  - condition: state
    state: 'on'
    entity_id: light.office
  action:
  - service: light.turn_off
    data: {}
    target:
      entity_id: light.office
  mode: single

As above, the issue with this is that it only triggers if the Motion has recently been triggered.
But if you turn the light on remotely or by mistake, this automation would never run.

So what I’m trying to achieve is, to turn off the light if it has been on for X and no motion for X.

  • Has the light been turned on for X minutes?
    –NO = End.
    –YES = Continue
  • Has there been motion within the last X minutes?
    –YES = End
    –NO = Continue > Turn light off

Thinking further about this I see what you mean, it might be a little trickier to run off “how long the light has been on” vs “when was there last movement”

These have been running without issues for years, it’s just whilst away and I’ll turn a light on in a room for CCTV I forgot to turn it off.
I set up an alternative with pings me a notification if a light has been on for longer than X then I can turn it off via the notification.

I’ll probably come back to this another time.