Simplify 3 automations to 1

Heyho :slight_smile:

Is there any way to simplify those 3 automations into 1?

- trigger:
    platform: event
    event_type: deconz_event
    event_data:
      id: osram_mini_wohnzimerrolladen
      event: 1002
  action:
    service: cover.open_cover
    target:
      entity_id: cover.wohnzimmer_door_cover
      
- trigger:
    platform: event
    event_type: deconz_event
    event_data:
      id: osram_mini_wohnzimerrolladen
      event: 3002
  action:
    service: cover.stop_cover
    target:
      entity_id: cover.wohnzimmer_door_cover
      
- trigger:
    platform: event
    event_type: deconz_event
    event_data:
      id: osram_mini_wohnzimerrolladen
      event: 2002
  action:
    service: cover.close_cover
    target:
      entity_id: cover.wohnzimmer_door_cover

I don’t use event triggers very often, but I think the following should work:

- trigger:
    platform: event
    event_type: deconz_event
    event_data:
      id: osram_mini_wohnzimerrolladen
  action:
    service: |
      cover.{{ 'open' if trigger.event.data.event == '1002' else 'stop' if trigger.event.data.event == '3002' else 'close' if trigger.event.data.event == '2002'}}_cover
    target:
      entity_id: cover.wohnzimmer_door_cover

If there are other events from that same id, you may get some warnings from calling an invalid service. So, you may need to set a condition to avoid those triggers.

condition: "{{trigger.event.data.event in ['1002', '2002', '3002'] }}"

Thank you very much :slight_smile:

The final version now is this:

- alias: Wohnzimmerolladensteuerung
  trigger:
    platform: event
    event_type: deconz_event
    event_data:
      id: osram_mini_wohnzimerrolladen
  condition: "{{trigger.event.data.event in [1002, 2002, 3002] }}"
  action:
    service: |
      cover.{{ 'open' if trigger.event.data.event == 1002 else 'stop' if trigger.event.data.event == 3002 else 'close' if trigger.event.data.event == 2002}}_cover
    target:
      entity_id: cover.wohnzimmer_door_cover

Just had to remove the apostroph around the numbers. (I guess because it was an int not a String)

In any case, much cleaner :slight_smile: Thanks again ^^

It would be easier to use a map and you’d want to ensure the type because a string and an int are not the same.

This defaults to stop if it get’s invalid commands, ensure’s that the incoming event data will pass an == or .get command, and is alittle easier to read than a single line nested if statements. Typically you don’t want to nest multiple if statements in a single line because it’s easy to misunderstand when you come back to it later.

    service: >
      cover.{{ {1002:'open', 3002:'stop', 2002:'close'}.get(trigger.event.data.event | int(3002), 'stop') }}_cover

if this were my automation, I’d do it this way so that debugging is easier in traces and you can view each step of automation from the trigger position.

- trigger:
    platform: event
    event_type: deconz_event
    event_data:
      id: osram_mini_wohnzimerrolladen
  variables:
    event: "{{ trigger.event.data.event }}"
    events:
      1002: open
      2002: close
      3002: stop
    service: "{{ events.get(event) }}"
    valid_event: "{{ service is not none }}"
  condition:
  - condition: template
    value_template: "{{ valid_event }}"
  action:
    service: cover.{{ service }}_cover
    target:
      entity_id: cover.wohnzimmer_door_cover

EDIT: somehow I missed @WolfsW3lp3’s post. Disregard if this if you’re happy with what you have.

1 Like