I have several lights which turn on/off depending on some movement sensors, and they all use a single input_number field to set the brightness.
I’d like to make that brightness level different depending on the day hour, which is possible using a data_template field in the turn_on trigger but I don’t want to repeat the same conditional code on each automation.
Is is possible to create a snippet and reuse it? Should I create a trigger for each day hour I’d like to change the level and adjust the input_number value so the rest of my setup is still the same? Is there any other option?
Sorry for the misunderstanding: I have different sensors which turn on different lights in different automations, not just one.
My setup is something like:
a boolean to make the dinning room automation effective: “enable turn on/off dinning room”
an input number with the minimal light level to run the automation: 20 lx
an input number with the desired brightness level: 100% (which is later multiplied by 2.55 and then rounded)
an automation which triggers when there is movement in the dinning room and checks for the minimal light level and the boolean and if everything is fine, then turns on the dinning room light with the desired brightness
This setup is repeated for the corridor and the entrance of the house, which means a new boolean for each location and an automation, but all of them use the same two input numbers.
What I don’t want to repeat is the conditional template for each automation to choose a brightness level depending on the time.
Actually I’m trying a new automation to set the input_number value hourly, without success (yet):
- alias: 'Ajusta brillo segun la hora'
trigger:
- platform: time
minutes: '/60'
action:
- service: input_number.set_value
data_template:
entity_id: input_number.custom_brightness
value: >
{%- if now().strftime('%H')| int >= 21 %}
20
{%- elif now().strftime('%H')| int < 7 %}
20
{%- elif now().strftime('%H')| int >= 7 %}
100
{% endif %}
FYI, the light.turn_on service accepts either brightness or brightness_pct, so you can just use the percentage value directly (with brightness_pct) without having to scale it to 255.
For the automation that adjusts input_number.custom_brightness, you can use now().hour, e.g. {% if now().hour >= 21 %}.
For the trigger, I think you want:
trigger:
platform: time
minutes: 0
seconds: 0
So…
- alias: 'Ajusta brillo segun la hora'
trigger:
platform: time
minutes: 0
seconds: 0
action:
service: input_number.set_value
data_template:
entity_id: input_number.custom_brightness
value: "{{ 20 if now().hour < 7 or now().hour >= 21 else 100 }}"
Actually, it really just needs to change the level twice, so:
- alias: 'Ajusta brillo segun la hora'
trigger:
- platform: time
at: '07:00:00'
- platform: time
at: '21:00:00'
action:
service: input_number.set_value
data_template:
entity_id: input_number.custom_brightness
value: "{{ 100 if trigger.now.hour == 7 else 20 }}"
One more suggestion. As I said above, you really don’t need to run the automation every hour to adjust input_number.custom_brightness. Given what you have, it seems you want to change it to 100 at 07:00, and to 20 at 21:00.
You might also want to make sure that it’s the desired value when HA starts. E.g., if HA is down at one of the adjustment times, either due to a power outage, or for maintenance, it would miss the adjustment. But you can add an event to the automation so it sets the correct value when HA starts. I’ve done this in many of my own automations. So…
- alias: 'Ajusta brillo segun la hora'
trigger:
- platform: time
at: '07:00:00'
- platform: time
at: '21:00:00'
- platform: homeassistant
event: start
action:
- service: input_number.set_value
data_template:
entity_id: input_number.custom_brightness
value: "{{ 20 if now().hour < 7 or now().hour >= 21 else 100 }}"
This might work, i changed your shut off time to occur when 10 minutes of no movement. You can put automation.Enciende_pasillo_automaticamente to control it from the front end.
automation Enciende_pasillo_automaticamente:
alias: Enciende_pasillo_automaticamente
trigger:
- platform: state
entity_id: binary_sensor.motion_sensor_pasillo
to: 'on'
- platform: state
entity_id: binary_sensor.motion_sensor_pasillo
to: 'off'
for:
minutes: 10
action:
- service_template: 'light.turn_{{trigger.to_state.state}}'
entity_id: light.pasillo
data_template:
brightness_pct: "{{ (20 if now().hour < 7 or now().hour >= 21 else 100) | int }}"
Sure but this option seems to have “more” maintenance work when adding more hours: adding new hours becomes in a change in trigger and data_template. As I will surely add a third option soon I decided to run it hourly and adjust the hours only in the template code.
Right, and that was my first approach, but with a boolean I can control both automations (turn on and turn off) with a single control instead of turning on / off both automations. It seems to be the same for your “turn off outside lights”: with one boolean “turn off outside automatically” you can disable both of them.