Reusable data_template?

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?

Thanks in advance.

The light.turn_on service can accept a list of lights to turn on, and you can then just use one brightness value. E.g.:

  ...
  action:
    - service: light.turn_on
      entity_id: light.light1, light.light2, light.light3
      data_template:
        brightness: "{{ some_expression_that_determines_desired_brightness_level }}"

Or does each light respond to a different sensor, and hence, needs its own automation?

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

Thanks for the clarifications.

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 }}"
1 Like

Thanks for the help.

Here’s the final code (for a single room) for anyone who has the same needs.

    input_boolean:
      manage_pasillo:
        name: Encender/Apagar pasillo automaticamente
        initial: on

    input_number:
      min_illumination_level:
        name: Luz minima
        unit_of_measurement: lx
        initial: 20
        min: 0
        max: 200
        step: 10
        icon: 'mdi:white-balance-sunny'

      custom_brightness:
        name: Brillo Luz
        unit_of_measurement: "%"
        initial: 100
        min: 0
        max: 100
        step: 10
        icon: 'mdi:lightbulb-on-outline'

    automation:
      - alias: 'Ajusta brillo segun la hora cada 60 minutos'
        trigger:
          - platform: time
            minutes: 0
            seconds: 0
        action:
          - service: input_number.set_value
            data_template:
              entity_id: input_number.custom_brightness
              value: >
                {% if now().hour >= 21 -%}
                  20
                {% elif now().hour < 7 -%}
                  20
                {% elif now().hour >= 7 -%}
                  100
                {% endif %}

      - alias: 'Enciende pasillo automaticamente'
        trigger:
          - platform: state
            entity_id: binary_sensor.motion_sensor_pasillo
            to: 'on'
        condition:
          condition: and
          conditions:
            - condition: state
              entity_id: input_boolean.manage_pasillo
              state: 'on'
            - condition: template     
              value_template: "{{ states('sensor.illumination_pasillo') | int < states('input_number.min_illumination_level') | int }}"
        action:
          - service: light.turn_on
            entity_id: light.pasillo
            data_template:
              brightness_pct: "{{ states('input_number.custom_brightness') | int }}"
      
      - alias: 'Apaga pasillo automaticamente'
        trigger:
          - platform: state
            entity_id: binary_sensor.motion_sensor_pasillo
            to: 'off'
        condition:
          condition: and
          conditions:
            - condition: state
              entity_id: input_boolean.manage_pasillo
              state: 'on'
        action:
          - service: light.turn_off
            entity_id: light.pasillo
1 Like

You could have one trigger that listens to state change event, and in that, you can check what triggered the event, and write actions accordingly.

You can access the data using trigger.event.data.

Here is an example: https://github.com/skalavala/smarthome/blob/3fa4e6679d8213bb371dad5fe5db421988e13f71/python_scripts/batteries.py#L35-L37

That sounds good also to avoid copied/pasted code in automations. I’ll take a look also.

I don’t think you want this. Since you haven’t specified seconds, this will trigger each second during that minute. You really want:

  trigger:
    platform: time
    minutes: 0
    seconds: 0

You’re right, it’s in the docs but haven’t noticed the warning.

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

I would also just turn on or off the automation itself instead of creating a input for it.

image

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.

That’s something I’ve been trying to fix: if I miss the triggering event for some reason, the lights never turn off. Thanks for the suggestion.

To follow up on my posts above (about trigger.now), the behavior of that variable changed with HA release 0.81. See this post for more details.