Automation on multiple entities

...automation.yaml

- alias: "Set Brightness Level from Slider"
  hide_entity: False
  trigger:
    platform: state
    entity_id: input_slider.lights_brightness_level
  action: >-
      service: light.turn_on
      {%- for state in states.light if state.state == "on" -%}
        entity_id: {{ state.entity_id }}
        data_template:
          brightness: {{ (trigger.to_state | int * 255 / 100) | round }}
      {%- endfor -%}

I have this automation, but it keeps throwing an error. Am I misunderstanding how to use templates? Essentially I’m trying to make a brightness slider, such that whenever it is moved, it turns all lights that are ON to that level of brightness.

Templates work only within values (and then only values that support templating), not as wrappers for code within YAML.

That’s a shame since that would be an awesome automation. :slight_smile:

Do you think this sort of automation could be done then? I have a great understanding of python, but it doesn’t exactly convert into templating. There’s a TON of potential within the current automation and templating system, but I feel like if it was supported for all parts of the automation, that would bring automation to an entirely different level.

I’m sure I could do this for all lights, but if the light is off, I don’t want it being turned on.

Looks like you might be a good candidate for AppDaemon. It’s HASS event module for Python programmers.

1 Like

This sounds like a great alternative! I’ve just got to get it working now. Thank you ih8gates!

& @ih8gates

Heyo! I got this working pretty simply when I was able to play around in python. I’m much more comfortable there.

- alias: "Set Brightness Level from Slider"
  hide_entity: False
  trigger:
    platform: state
    entity_id: input_slider.lights_brightness_level
  action: >-
      service: light.turn_on
      {%- for state in states.light if state.state == "on" -%}
        entity_id: {{ state.entity_id }}
        data_template:
          brightness: {{ (trigger.to_state | int * 255 / 100) | round }}
      {%- endfor -%}

turned very easily into …

class SliderBrightnessLevel(appapi.AppDaemon):

    def initialize(self):
        self.listen_state(self.adjust_bright_level, 'input_slider.lights_brightness_level')
                                    #entity can be just "light" to listen to all lights

    def adjust_bright_level(self, entity, attribute, old, new, kwargs):
        self.log("{}.{} changed from {} to {}.".format(entity, attribute, old, new))

        for entity_id, state in self.get_datapoint_from_all(datapoint='state', service='light'):
            if state == 'on':

                # brightness is in 0 - 255, constrain to 255 to get percentage-of
                blevel = round(int(float(new)) * 255 / 100)
                
                self.log("light.{} set to {}.".format(entity_id, blevel))
                self.turn_on(entity_id, brightness=blevel)
    
    # helper functions
    def get_datapoint_from_all(self, datapoint, service):
        all_things = self.get_state(service)

        for entity_id in all_things:
            for data in all_things[entity_id]:
                if data == datapoint:
                    yield entity_id, all_things[entity_id][data]

and the panel I control it from looks like this. :slight_smile:

2 Likes