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?
Glad you figured out the cause and were able to get the switch to update correctly
Can you test each part of the templates in the dev tools template editor individually to verify they are giving the expected results?
Does this result in true or false during the proper conditions? It should be true when the light hasn’t been adjusted for greater than duration set on the slider. It should be false if it was adjusted more recently than the time set on the slider.
As far as I can tell the parts are working as expected. Though the “last_update” (your first condition) seems to be returning true regardless but I’m wondering if that’s just a lag in my changing the state and that state updating in HASS since I have the slider at 1 minute. Pics:
So it should be triggering if changing from false to true. Are you able to cause conditions to make it result in false? I’m wondering if for some reason they are always returning true. It shouldn’t trigger unless moving from false to true. Also, we could try to take the input slider out for testing to simplify.
If you want to set a fixed value in seconds instead of using the slider, replace “(states.input_slider.dr_off_delay.state|float * 60)” with the number of seconds.
Sorry for the late response. Just came across another post that made me think of your situation. Part of the post said, [quote=“ih8gates, post:2, topic:12595”]
I have lots of trouble getting template triggers to work for me.
[/quote]
and
Any errors in your log that might have to do with the automation? I noticed a hyphen in front of platform in the trigger that isn’t needed because it is’nt a list. Might try removing that and verifying the formatting before trying the suggestion below.
The the suggestion above didn’t work, maybe the following will. Still trial and error like before so not sure if this will do it. Fingers crossed.
sensor:
- platform: template
sensors:
brightness_trigger:
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) }}'
friendly_name: 'Sun angle'
- alias: 'Turn off dining room light when at <50 brightness for set duration'
trigger:
platform: state
entity_id: sensor.brightness_trigger
to: 'true'
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