Multiple binary triggers in one automation to control on/off/dimming of light

So I have an IHC controller which controls some of my light and I also use it to turn some Ikea bulbs On/off and dimming.

Today I do this in 4 automations, but I would like to make it into one automation and create a blue print.

The 4 automations:

- alias: Turn on bryggers light
  trigger:
    platform: state
    entity_id: binary_sensor.light_bryggers_turn_on
    from: 'off'
    to: 'on'
  action:
  - service: light.turn_on
    entity_id: light.bryggers1_level_on_off
    data:
      brightness: 190

- alias: Turn off bryggers light
  trigger:
    platform: state
    entity_id: binary_sensor.light_bryggers_turn_off
    from: 'off'
    to: 'on'
  action:
  - service: light.turn_off
    entity_id: light.bryggers1_level_on_off

- alias: Increase bryggers light
  trigger:
    platform: state
    entity_id: binary_sensor.light_bryggers_increase_brightness
    from: 'off'
    to: 'on'
  action:
  - service: light.turn_on
    entity_id: light.bryggers1_level_on_off
    data_template:
      brightness: '{{ ([states.light.bryggers1_level_on_off.attributes.brightness
        + 20, 255] | min) | int }}'

- alias: Decrease brightness bryggers
  trigger:
    platform: state
    entity_id: binary_sensor.light_bryggers_decrease_brightness
    from: 'off'
    to: 'on'
  action:
  - service: light.turn_on
    entity_id: light.bryggers1_level_on_off
    data_template:
      brightness: '{{ ([states.light.bryggers1_level_on_off.attributes.brightness
        - 20, 5] | max) | int }}'

Now I have tried to create this using service template and data template, but I’m doing something wrong. Can somebody point me in the right direction?

- alias: Bryggers light switch
  trigger:
    - platform: state
      entity_id: binary_sensor.light_bryggers_turn_on
      from: 'off'
      to: 'on'
    - platform: state
      entity_id: binary_sensor.light_bryggers_turn_off
      from: 'off'
      to: 'on'
    - platform: state
      entity_id: binary_sensor.light_bryggers_increase_brightness
      from: 'off'
      to: 'on'
    - platform: state
      entity_id: binary_sensor.light_bryggers_decrease_brightness
      from: 'off'
      to: 'on'
  action:
    service_template: >
      {% if trigger.to_state.state == "on" %}
      light.turn_on
      {% elif trigger.to_state.state == "off" %}
      light.turn_off
      {% endif %}
  data_template: 
    entity_id: >
      {% if trigger.from_state.entity_id == "binary_sensor.light_bryggers_turn_on" %}
      light.bryggers1_level_on_off
        brightness: 190
      {% elif trigger.from_state.entity_id == "binary_sensor.light_bryggers_turn_off" %}
      light.bryggers1_level_on_off
      {% elif trigger.from_state.entity_id == "binary_sensor.light_bryggers_increase_brightness" %}
      light.bryggers1_level_on_off
        brightness: '{{ ([states.light.bryggers1_level_on_off.attributes.brightness + 20, 255] | min) | int }}'
      {% elif trigger.from_state.entity_id == "binary_sensor.light_bryggers_decrease_brightness" %}
      light.bryggers1_level_on_off
        brightness: '{{ ([states.light.bryggers1_level_on_off.attributes.brightness - 20, 5] | max) | int }}'
      {% endif %} 

Do you specifically want to use templates here as you could always group all the triggers like you have in the 2nd example and simply use the choose action and just separate the actions based on the triggers so still achieving one automation.

I tend to use the choose option, probably too much but it’s because for me it’s easier to understand and to a certain extent easier to debug the automation or script graphically and I am pants at templates as well :man_shrugging:

I have no desire to use templates in particular, I was not aware that it could be done differently.

Could post an example?

Thanks.


- alias: Bryggers light switch
  trigger:
    - platform: state
      entity_id: binary_sensor.light_bryggers_turn_on
      from: 'off'
      to: 'on'
      id: turn_on
    - platform: state
      entity_id: binary_sensor.light_bryggers_turn_off
      from: 'off'
      to: 'on'
      id: turn_off
    - platform: state
      entity_id: binary_sensor.light_bryggers_increase_brightness
      from: 'off'
      to: 'on'
      id: turn_on
    - platform: state
      entity_id: binary_sensor.light_bryggers_decrease_brightness
      from: 'off'
      to: 'on'
      id: turn_on
  condition: []
  action:
    - choose:
      - conditions:
        - condition: trigger
          id: turn_off
        sequence:
        - service: light.turn_off
          data: []
          target:
            entity_id: light.bryggers1_level_on_off
      - conditions:
        - condition: trigger
          id: turn_on
        - condition: template
          value_template: "{{ valid_service }}"
        sequence:
        - service: light.turn_on
          data:
            brightness: "{{my_brightness}}"
          target:
            entity_id: light.bryggers1_level_on_off
    mode: single
    variables:
      key: "{{ trigger.entity_id }}"
      pairs:
        binary_sensor.light_bryggers_turn_on: 190
        binary_sensor.light_bryggers_increase_brightness: "{{([states.light.bryggers1_level_on_off.attributes.brightness + 20, 255] | min) | int )}}"
        binary_sensor.light_bryggers_decrease_brightness: "{{([states.light.bryggers1_level_on_off.attributes.brightness - 20, 5] | max) | int )}}"
      my_brightness: "{{ pairs.get(key) }}"
      valid_service: "{{ my_brightness is not none }}"

For your consideration, a distilled version (untested):

- alias: Bryggers light switch
  variables:
    lvl: "{{ states_attr('light.bryggers1_level_on_off', 'brightness') }}"
    pairs:
      turn_on: 190
      turn_off: 0
      increase_brightness: '{{ [lvl + 20, 255] | min }}'
      decrease_brightness: '{{ [lvl - 20, 5] | max }}'
    my_brightness: '{{ pairs.get(trigger.to_state.object_id[15:]) }}'
  trigger:
    - platform: state
      entity_id:
        - binary_sensor.light_bryggers_turn_on
        - binary_sensor.light_bryggers_turn_off
        - binary_sensor.light_bryggers_increase_brightness
        - binary_sensor.light_bryggers_decrease_brightness
      from: 'off'
      to: 'on'
  condition:
    - '{{ my_brightness is not none }}'
  action:
    - service: light.turn_on
      data:
        brightness: '{{ my_brightness }}'
      target:
        entity_id: light.bryggers1_level_on_off
  mode: single

EDIT

FWIW, I have never defined a variable outside the action that references the trigger variable. I usually define it within the action so this is a first for me.

I’m getting a “duplicated mapping key” for the second “conditions” in the automation.

Sorry about that. It looks like I had a couple typos… I corrected them in the original post.

No worries. I will try it out when I get home from work and thanks.

I tried this out and no error in the automation. But there is no control of the lights.

The example I posted is a shrunken version of Didgeridrew’s automation. I may have made a mistake in the process of shrinking it. Use Didgeridrew’s version.

I keep getting this error:
Invalid config for [automation]: template value should be a string for dictionary value @ data[‘action’][0][‘choose’][0][‘sequence’][0][‘data’]. Got None. (See /config/configuration.yaml, line 29).

So there is something it does not like, but I cant figure out the issue.

So I had some issue getting both codes to work, so I ended up with something in between both suggestions. Would be nice if it could be even shorter.

- alias: Bryggers light switch
  trigger:
    - platform: state
      entity_id:
        - binary_sensor.light_bryggers_turn_on
        - binary_sensor.light_bryggers_turn_off
        - binary_sensor.light_bryggers_increase_brightness
        - binary_sensor.light_bryggers_decrease_brightness
      from: 'off'
      to: 'on'
  condition: []
  action:     
  - choose:              
      - conditions:
          - condition: template
            value_template: "{{ ( trigger.entity_id ) == ( 'binary_sensor.light_bryggers_turn_on' ) }}" 
        sequence:
          - service: light.turn_on
            entity_id: light.bryggers1_level_on_off
            data:
              brightness: 190
      - conditions:
          - condition: template
            value_template: "{{ ( trigger.entity_id ) == ( 'binary_sensor.light_bryggers_turn_off' ) }}" 
        sequence:
          - service: light.turn_off
            entity_id: light.bryggers1_level_on_off
      - conditions:
          - condition: template
            value_template: "{{ ( trigger.entity_id ) == ( 'binary_sensor.light_bryggers_increase_brightness' ) }}" 
        sequence:
          - service: light.turn_on
            entity_id: light.bryggers1_level_on_off
            data_template:
              brightness: '{{ ([states.light.bryggers1_level_on_off.attributes.brightness
              + 20, 255] | min) | int }}'
      - conditions:
          - condition: template
            value_template: "{{ ( trigger.entity_id ) == ( 'binary_sensor.light_bryggers_decrease_brightness' ) }}" 
        sequence:
          - service: light.turn_on
            entity_id: light.bryggers1_level_on_off
            data_template:
              brightness: '{{ ([states.light.bryggers1_level_on_off.attributes.brightness
              - 20, 5] | max) | int }}'