Quantcast
Channel: Configuration - Home Assistant Community
Viewing all articles
Browse latest Browse all 95919

Trigger on event state_change -- any way to filter?

$
0
0

@jwelter wrote:

I have the below automation that works fine. It uses conditions to only trigger on the desired state events.

But is there a better way to do this? I tried using a template trigger (the condition that counts the lights on) and use that as a trigger when it changes but that didn’t work well – too much lag.

It just seems dirty to trigger on all state changes but perhaps I am over-thinking this.

##########################################################
## Update brightness when light turned on
##########################################################
- alias: Adjust brightness as lights are turned on
  initial_state: True

  trigger:
    - platform: event
      event_type: state_changed
    
  condition:
    # Only look at lights going off to on
    - condition: template
      value_template: "{{ trigger.event.data is defined }}"

    - condition: template
      value_template: "{{ trigger.event.data.entity_id.split('.')[0] == 'light' }}"
    
    - condition: template
      value_template: "{{ trigger.event.data.new_state.state == 'on' }}"
      
    - condition: template
      value_template: "{{ trigger.event.data.old_state.state == 'off' }}"
   
    # Don't change these lights.
    #   - condition: template
    #     value_template: >-
    #       {{ trigger.event.data.entity_id != 'switch.outdoor_front_switch' }}
   
    - condition: template
      value_template: "{{ states.sensor.target_brightness_inside.state is defined }}"

    - condition: template
      value_template: "{{ states.light | selectattr('state','==','on') | map(attribute='entity_id') | list | count >= 1 }}"

  action:
    - service: light.turn_on
      data_template:
        entity_id: >-
          {{ states.light | selectattr('state','==','on') | map(attribute='entity_id') | join(',') }}
        brightness_pct: >-
          {{ states('sensor.target_brightness_inside')|int }}
        transition: 2

Posts: 2

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 95919

Trending Articles