I have a (hopefully) rather simple question. The kids don’t seem to fully shut off the dining room light and leave it just a little bit on. I want to have an automation that after say 10 minutes of <50 brightness will turn it off. Is there a way to accomplish this? The dimmer is z-wave and integrated into HASS. Is there a way to make this work? My searches have turned up nothing.
Hey @Kbeesnees, I would love too, but honestly I don’t even know where to start. Obviously a trigger is if brightness < 50. But no clue about for a period of time. Then the action is pretty simple, just a light off.
This code is not tested but something like this might work…
- alias: Lights off after 10 minutes and 50 brightness
trigger:
- platform: state
entity_id: light.entity_id
condition:
condition: template
data_template: '{{ states.light.entity_id.attributes.brightness | float < 50 }}'
for:
minutes: 10
action:
service: homeassistant.turn_off
entity_id: light.entity_id
Not sure if a brightness change would register as a state change since its technically an attribute… If not, the trigger would have to be modified to register a brightness change.
Thanks sir, going to do some testing tomorrow. I need to grab something to eat though and head to bed before too long. Appreciate your assistance! I’ll let you know if I run into any problems.
Hey @Kbeesnees. So I’ve played around with the automation and have gotten close. This works, however it doesn’t have the 5 minute delay:
- alias: 'Turn off dining room light when <50 brightness for 5 minutes'
trigger:
- platform: state
entity_id: light.dining_room_light_level_11_0
condition:
condition: template
value_template: '{{ states.light.dining_room_light_level_11_0.attributes.brightness | float < 50 }}'
action:
service: light.turn_off
entity_id: light.dining_room_light_level_11_0
As soon as I add in the:
for:
minutes: 5
It blows up with this in the log:
17-01-31 19:48:28 ERROR (MainThread) [homeassistant.bootstrap] Invalid config for [automation]: extra keys not allowed @ data['condition'][0]['for']. Got None
not a valid value for dictionary value @ data['condition'][0]['condition']. Got None
required key not provided @ data['condition'][0]['entity_id']. Got None. (See /home/hass/.homeassistant/configuration.yaml, line 421). Please check the docs at https://home-assistant.io/components/automation/
I’ve also noticed that the brightness attribute only exists when the dimmer is on. With it off that attribute is missing. Not sure if that’s a problem of not, though I’m sure it would be easy enough to add in an if.
As far as the brightness attribute not existing when the light is off, the automation might try to run one more time after it turned it off the first time because of the state change. Shouldn’t really be an issue because the brightness attribute doesn’t exist so it will stop at the condition. Not sure if it will put an error in your log but it shouldn’t actually be an issue.
If the brightness attribute trigger works though, you may be able to add the "| float < 50 " to the end of it like I have put in my examples. That should prevent the error that might happen if no brightness attribute exists because the automation should only trigger if the brightness attribute exists and is below 50%. You may have to experiment by adding something like “to: ‘true’” under the condition.
If you don’t use the trigger template for brightness attribute and once you get the “for” working, try to set the brightness to greater than 50% for longer than your “for” duration. Then lower the brightness to less than 50% for at least your “for” duration. I suspect by using the state as the trigger, it won’t run the automation. That is why I was using the brightness template as the trigger. I’m not sure but I don’t believe a brightness change constitutes a state change and that is what your trigger is looking for.
I noticed in your first post you said 10 minutes so if you want to have a variable slider timer, this might work as well. As it is, the brightness level will have to be less than 50% for up to 20 minutes and the slider can be adjusted in 30 second increments down to 0.
Sorry I just got back to looking into this. I’m still having issues with the for statement. This appears to work (had to quote the alias and change the condition from ‘data_template’ to ‘value_template’) - at least it’s not erroring out. I’m not home to check how the light is behaving.
As soon as I comment out the for lines though it throws a fit:
17-02-02 16:28:58 ERROR (MainThread) [homeassistant.bootstrap] Invalid config for [automation]: extra keys not allowed @ data['condition'][0]['for']. Got None
not a valid value for dictionary value @ data['condition'][0]['condition']. Got None
required key not provided @ data['condition'][0]['entity_id']. Got None. (See /home/hass/.homeassistant/configuration.yaml, line 426). Please check the docs at https://home-assistant.io/components/automation/
If I leave the condition as a data_template I get another error:
17-02-02 16:27:34 ERROR (MainThread) [homeassistant.bootstrap] Invalid config for [automation]: extra keys not allowed @ data['condition'][0]['data_template']. Got None
not a valid value for dictionary value @ data['condition'][0]['condition']. Got None
required key not provided @ data['condition'][0]['entity_id']. Got None. (See /home/hass/.homeassistant/configuration.yaml, line 426). Please check the docs at https://home-assistant.io/components/automation/
For template triggers and conditions, the right keyword is always value_template, never data_template.
Also, only the state trigger and condition support the for: statement. All the other triggers and conditions do not.
I worked around this current limitation using a threshold binary sensor. In my use case, the state of this sensor is based on the luminance in my office:
I’m obviously not the best with templates but they are so powerful. It takes me some trial and error to get them working. I was working this out as well during the above conversation. You could try…
input_slider:
- off_delay:
name: Off Delay
min: 0
max: 20
step: .5
automation:
- alias: 'Turn off dining room light when at <50 brightness for set duration'
trigger:
- platform: template
value_template: '{{ (as_timestamp (now()) -as_timestamp (states.light.dining_room_light_level_11_0.last_updated))|float >= (states.input_slider.off_delay.state|float * 60) }}'
condition:
condition: template
value_template: '{{ states.light.dining_room_light_level_11_0.attributes.brightness | float <= 50 }}'
action:
service: light.turn_off
entity_id: light.dining_room_light_level_11_0
@fanaticDavid, I didn’t realize “for” has those limitations. Thanks for this piece of info. Also didn’t realize (never thought about it) the difference between data_template and value_template. It makes sense now. I copied some of that code from another post somewhere and I’m sure they were using it correctly. Thanks again. Always learning, often from you.
If you don’t want the slider, use this for your trigger…
It is set at 300 seconds i.e. 5 minutes
@mrtips, @fanaticDavid, and @Kbeesnees I think these suggestions should get me up and running, thanks! Time to do some testing and see if I can get a working automation. I’ll make sure to post what I end up using. Thanks for all your help!!!
@Kbeesnees I’m FINALLY circling back to this one and am still unable to get it working. The switch is updating status as expected now (YAY!) but the automation still isn’t doing it’s job. I’m using your automation from the other thread:
- alias: 'Turn off dining room light when at <50 brightness for set duration'
trigger:
- platform: template
value_template: '{{ (as_timestamp (now()) -as_timestamp (states.light.dining_room_light_level_11_0.last_updated))|float >= (states.input_slider.off_delay.state|float * 60) and (states.light.dining_room_light_level_11_0.attributes.brightness | float <= 50) }}'
condition:
condition: template
value_template: '{{ states.light.dining_room_light_level_11_0.attributes.brightness | float <= 50 }}'
action:
service: light.turn_off
entity_id: light.dining_room_light_level_11_0
I have the slider set to 1 minute just for easy testing. I put it all the way one for a few minutes, then took it way down to like 10 brightness. Left it there almost 10 minutes and it never turned off. Any other thoughts?