Turn on an off light with specific brightness / change brightness from sensor

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?

 - alias: Driveway light
   trigger:
     - platform: sun
       event: sunset
     - platform: sun
       event: sunrise
   action:
     service_template: >
       {% if trigger.event == 'sunset' %}
       light.turn_on
       {% elif trigger.event == 'sunrise' %}
       light.turn_off
       {% endif %}

     entity_id: light.ute_2
     data:
       brightness: 160

I know this would be simple with two automations but i guess it will be doable within one automation?

The next step is to make a binary sensor change the brightness when the light is on.

 - alias: Driveway light  motion
   trigger:
     - platform: state
       entity_id: binary_sensor.ip_camera_line_crossing

   condition:
     condition: state
     entity_id: light.ute_2
     state: 'on'

   action:
     service_template: >
       {% if trigger.to_state.state == 'on' %}
       light.turn_on
       {% elif trigger.to_state.state == 'off' %}
       light.turn_on
       {% endif %}
     entity_id: light.ute_2
     data_template:
       brightness: >
          {% if trigger.to_state.state == 'on' %}
          250
          {% elif trigger.to_state.state == 'off' %}
          160
          {% endif %}

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? =)

What do you think?

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. :slight_smile:

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.

This won’t work.
Getting this error:

Invalid service data for light.turn_on: expected int for dictionary value @ data['brightness']. Got '{{ 250 if trigger.to_state.state == "on" else 160 }}'

You can probably use a couple Scenes with one Automation, Example.

Shouldn’t this be "trigger.to_state == “on”?

Do you have that under data_template or data? It has to be under data_template. I.e.:

service: light.turn_on
entity_id: light.ute_2
data_template:
  brightness: '{{ 250 if trigger.to_state.state == "on" else 160 }}'
1 Like

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.

:+1: Glad I asked, still on the curve.

1 Like

Not that far ahead of you on that curve. :slight_smile:. Check out:

https://www.home-assistant.io/docs/automation/templating/#trigger-state-object

Lol! That’s the same link you already gave!

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 }}'

What do mean?

Sorry for beeing unclear in my latest post. I was very tired =).
I am talking about this, my first problem in the first post.

 - alias: Driveway light
   trigger:
     - platform: sun
       event: sunset
     - platform: sun
       event: sunrise
   action:
     service_template: >
       {% if trigger.event == 'sunset' %}
       light.turn_on
       {% elif trigger.event == 'sunrise' %}
       light.turn_off
       {% endif %}

     entity_id: light.ute_2
     data:
       brightness: 160

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.

 - alias: Driveway light
   trigger:
     - platform: sun
       event: sunset
     - platform: sun
       event: sunrise
   action:
     service_template: >
       {% if trigger.event == 'sunset' %}
       light.turn_on
       {% elif trigger.event == 'sunrise' %}
       light.turn_on
       {% endif %}

     entity_id: light.ute_2
     data_template:
       brightness: '{{ 160 if trigger.event == "sunset" else 0 }}'

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’.

1 Like

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.

Found a solution in another topic which involves writing a simple python_script:

Thank you.
I will try this. :+1: