I have a light on my driveway. It is turned on at around 50% when the sun goes down.
But i can’t turn it off. This is because of the brightness set under the entity_id.
How can i keep this as one automation, set the brightness and get this working?
I know this is wrong but i hope it gives an idea of what i want.
Even this one is simple if splitted into two automations.
I try to keep the number of automations as low as possible,
And trying to learn something on the way.
And when the light will fall back to 160 brightness i want it to do it slow, possible? =)
Regarding your first question, the only way I can think to make it one automation is to use two scripts - one that is run for each case. One script turns off with no brightness value. The other turns on with a brightness value. But although that technically satisfies your requirement to do it in one automation, I doubt that adding two scripts to do it is what you were looking for.
The second one is easier. Change the service_template to just “service: light.turn_on”. Just because you need to use a template for the data does not mean you need to use a template for the service, too.
BTW, I would change the elif part to simply an else.
Oh, and actually, you can make the template for brightness much simpler:
brightness: '{{ 250 if trigger.to_state.state == "on" else 160 }}'
And I think (although I’ve never tried this myself), for the “do it slow” part you can add:
transition: 5
Or however many seconds you want it to take to transition to the new level.
Invalid service data for light.turn_on: expected int for dictionary value @ data['brightness']. Got '{{ 250 if trigger.to_state.state == "on" else 160 }}'
trigger.to_state is a state object, not a state. If you want the state, you need to use the state object’s .state attribute - i.e., trigger.to_state.state.
I am testing a new thing now to see if I solved turn on /turn off within one automation even with a specific brightness. Will see when the sun rises if it’s working.
This is the automation right now. I am very satisfied with it. The transition when dimming to 160 is so slow that my neighbours won’t notice it. Earlier they complaied about blinking lights. =)
- alias: Driveway light motion
trigger:
- platform: state
entity_id: binary_sensor.ip_camera_line_crossing
to: 'on'
- platform: state
entity_id: binary_sensor.ip_camera_line_crossing
to: 'off'
for: '00:05:00'
condition:
condition: state
entity_id: light.ute_2
state: 'on'
action:
service: light.turn_on
entity_id: light.ute_2
data_template:
brightness: '{{ 250 if trigger.to_state.state == "on" else 160 }}'
transition: '{{ 1 if trigger.to_state.state == "on" else 20 }}'
As said earlier, this won’t turn the light off due to the data>brightness.
What i tried here was this but it didn’t work.
I thought setting brightness to 0 would turn the light off.
So what happens when you try light.turn_on with a brightness value of 0? Also, what platform is this particular light? (I.e., z-wave, something else)?
I just tried your idea with a GE Z-Wave Plug-in Dimmer I have. I first called light.turn_on with a brightness value large enough to actually turn it on. I verified it changed to ‘on’ with that brightness value. Then I called light.turn_on with a brightness of 0. After the refresh delay, I verified it showed up as off.
I also looked at the turn_on and turn_off methods in the zwave light platform and verified that this should work. The only downside is that turning it “on” with a value of 0 will first change the entity’s state to ‘on’, but assuming you have the device configured w/ refresh_value set to true, or you have the device set up for polling, it will eventually show up as ‘off’.
This light is an ikea tradfri controlled through the deconz component.
With my automation above the light doesn’t turn off, it dims to brightness 1. The lamp is still turned on.
From what I can see, how the brightness value is interpreted / used is up to the individual light platform. For a Z-Wave dimmer, it looks like this (kind of) works, although probably not something to depend on. I started looking into the deconz code, but it just transfers the brightness value to the gateway, and I didn’t look any further (not having any of these, nor knowing much about them.)
I’m stumped. I don’t know of a way to conditionally include service data items, which is what it seems you need to do what you want. I think you’re stuck with needing two automations, or a single automation that calls two different scripts.