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’]
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:
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.
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.
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:
When the light has been on for X minutes (X is the input number), trigger
If the motion sensor has been off for X minutes - turn off the lights
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.