Understanding the use of templates

Hi all,

First off thanks to everyone on the community that is helping, I’ve found lots of really good information and explanations on here so far.

I’m still struggling with the concept of templates though. What I would like to achieve is to be able to control my lights individually with a brightness slider. I’ve done this using input numbers, but I’ve done this the long way by creating one per light. I also want to create an automation where I would like to control the brightness of all the lights in the house (I only have 3 so far) by using yet another input_number to control the all_lights group.

I suspect there is a much more efficient way of doing this, but am struggling a bit on how I can use templates to specify different lights to a single input_number configuration and what the configuration of this setup would be.

Thanks

Since you already have it working for each individual light, I would recommend adding an additional trigger (for your input_number that controls group.all_lights) to each automation.

automation:
  - alias: control_brightness
    trigger:
      - platform: state
        entity_id: input_number.light1_brightness
      - platform: state
        entity_id: input_number.all_lights_brightness
    condition:
      ...
    actions:
      ...

Thanks for that - can’t believe I hadn’t thought of simply modifying the trigger to the one automation config!

But does that still mean that I need to have multiple input_number configs for each one? If I end up getting more lights of if I want to re-use the already configured input_number slider for something else would I need to have another block of config for it? Am trying to think of the scalability of all my configs.

Thanks

Show us what you currently have and we can help you refine it :slight_smile:

Why are you using input numbers for this? Brightness is built into the light component.

Once you have light components properly set up, group them to control more than one a time.

1 Like

I know I can control the brightness of a light or a group of lights by clicking on the light (or group) and setting the brightness that way, but I wanted to have a slider straight on the card for the light similar to below as I think it just makes it more “user friendly” and I am lazy :slight_smile:

cards:
      - type: entities
        title: Lights
        show_header_toggle: false
        entities:
          - light.lounge_1
          - light.lounge_2
          - group.lounge_lights
          - input_number.lounge_brightness
          - light.upstairs_light
          - light.bedroom

As per my note to nickrout below, this is what I have:

input_number defined as:

  lounge_brightness:
    name:  Lounge Brightness
    initial: 100
    min: 5
    max: 100
    step: 5

and my automation is:

  - alias: control brightness
    trigger:
      - platform: state
        entity_id: input_number.lounge_brightness
    condition:
      - condition: state
        entity_id: group.lounge_lights
        state: 'on'
    action:
      service: homeassistant.turn_on
      data_template:
        entity_id: group.lounge_lights
        brightness_pct: "{{ trigger.to_state.state | int }}"

what I was hoping for was a way to re-use the same input number definition for all the automations, but the more I read about them and based on martokk’s comment I’ve come to get a better understanding and realise I don’t think its possible.

If I’ve understood it correctly, the only way to achieve something like:

  - alias: control brightness
    trigger:
      - platform: state
        entity_id: input_number.lounge_brightness
      - platform: state
        entity_id: input_number.lounge_1_brightness
      - platform: state
        entity_id: input_number.bedroom_brightness

would be to use a script or multiple automations as the conditions for each of those triggers and the actions would be different. Or am I still getting things totally wrong?

If it was me I would name the input_numbers the same as the lights and use one automation that maps the individual sliders to their light, and then use a second automation for the ‘master slider’ that switches off the first automation, sets all the lights, sets all the input_numbers and then turns the first one back on.

Something like:

input_number:
  lounge: #where light = light.lounge
    ...
  bedroom: #where light = light.bedroom
    ...

automation:
  - alias: control brightness
    trigger:
      platform: state
      entity_id:
        - input_number.lounge
        - input_number.bedroom
        - ETC
    action:
      service: homeassistant.turn_on
      data_template:
        entity_id: "light.{{ trigger.entity_id|replace('light.', '') }}" 
        brightness_pct: "{{ trigger.to_state.state | int }}"

  - alias: control master brightness
    trigger:
      platform: state 
      entity_id: input_number.light_all
    action:
      - service: homeassistant.turn_off
        entity_id: automation.control_brightness
      - service: light.turn_on
        data_template:
          entity_id: group.all_lights
          brightness_pct: "{{ trigger.to_state.state | int }}"
      - service: input_number.set_value
        data_template:
          entity_id:
            - input_number.lounge
            - input_number.bedroom
            - ETC
          number: "{{ trigger.to_state.state | float }}"
      - delay: 00:00:05
      - service: homeassistant.turn_on
        entity_id: automation.control_brightness

As an aside though, you can use custom_ui to put the actual light brightness slider on the card without all of this.

This custom card by @thomasloven for lovelace might be highly useful.

Thanks Dolores, that looks really good, but I prefer to use something a bit more mainstream than a proof of concept.

1 Like