Trigger ID doesn't work at all

Hello,

I am new to HA but I am reading a lot how I can transfer all my programs from Homematic to HA.

Yesterday I wrote a little program for a light in a small room. That room is that small so I only use a opening-sensor and the light. Door open = light on / door closed = light off.
This program does work, as long as I check against the lamp state itself in the action options. But I saw I can also use the trigger ID function to check against in the action options. So I changed the program, but now the lamp only turn on and never off again.

My question now, what did I do wrong, I don’t geht it. Here is the code of it:

alias: Vorratskammer-Licht
description: ""
trigger:
  - type: opened
    platform: device
    device_id: 8a437a78b21677cb748e5157b4d9b027
    entity_id: 76b40ab54f31e14f1303b353ee39b69e
    domain: binary_sensor
    id: opened
  - type: not_opened
    platform: device
    device_id: 8a437a78b21677cb748e5157b4d9b027
    entity_id: 76b40ab54f31e14f1303b353ee39b69e
    domain: binary_sensor
    id: closed
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - closed
        sequence:
          - type: turn_off
            device_id: 1716c7936d104be28b99f5e9fe061ead
            entity_id: 5ce214abf23c1496187d15730fb2532d
            domain: switch
      - conditions:
          - condition: trigger
            id:
              - opened
        sequence:
          - type: turn_on
            device_id: 1716c7936d104be28b99f5e9fe061ead
            entity_id: 5ce214abf23c1496187d15730fb2532d
            domain: switch
mode: single

Thanks for help!

You probably did what most people do, tried to trigger the automation from the GUI?

Anyways I suggest this setup instead:

alias: Vorratskammer-Licht
description: ""
trigger:
  - type: opened
    platform: device
    device_id: 8a437a78b21677cb748e5157b4d9b027
    entity_id: 76b40ab54f31e14f1303b353ee39b69e
    domain: binary_sensor
    id: on
  - type: not_opened
    platform: device
    device_id: 8a437a78b21677cb748e5157b4d9b027
    entity_id: 76b40ab54f31e14f1303b353ee39b69e
    domain: binary_sensor
    id: off
condition: []
action:
  - service: "switch.turn_{{ trigger.id }}"
    target:
      entity_id: switch.5ce214abf23c1496187d15730fb2532d
    data: {}
mode: single

Oh… and maybe you should name your entities something more useful. It will get messy very quickly

Since, you’re just starting out, here’s a friendly tip to avoid issues in the future Why and how to avoid device_ids in automations and scripts

2 Likes

Ah, ok, thanks for your hints. I know this with the devices / entity problem from Homematic, there I solved this with variables and only got one program per device to change the variable. I didn’t know the naming here but now I am a litte smarter :wink:

Anyway. I changed my program and now nothing works anymore:

alias: Vorratskammer-Licht-trigger-id
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.g_eg_vorrat_om_tur_0_g_eg_vorrat_om_tur
    attribute: value_state
    from: "on"
    to: "off"
    id: "0"
  - platform: state
    entity_id:
      - binary_sensor.g_eg_vorrat_om_tur_0_g_eg_vorrat_om_tur
    attribute: value_state
    from: "off"
    to: "on"
    id: "1"
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - "0"
        sequence:
          - type: turn_off
            device_id: 1716c7936d104be28b99f5e9fe061ead
            entity_id: 5ce214abf23c1496187d15730fb2532d
            domain: switch
      - conditions:
          - condition: trigger
            id:
              - "1"
        sequence:
          - type: turn_on
            device_id: 1716c7936d104be28b99f5e9fe061ead
            entity_id: 5ce214abf23c1496187d15730fb2532d
            domain: switch
mode: single

Yes, I creating this programs with the GUI. In the 21th century this should be possible, I got enough work with codes with my µc at work.
No, I didn’t test the program with the GUI. I walked down the stairs to test it :smiley:

The bottom half of your automation still uses devices.

Doesn’t explain why it doesn’t work, just wanted to point it out. You can change that to Call Service switch.turn_on (or off).

If you need help debugging, you need to post your automation traces from when it didn’t work.

BTW, this is redundant:

Trigger ids are numbered from 0 by default. So even if you left the ids out your triggers your automation would still work with the choose conditions you have.

You only need to specify the id if you want something other than numbered ids. e.g.

alias: Vorratskammer-Licht-trigger-id
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.g_eg_vorrat_om_tur_0_g_eg_vorrat_om_tur
    attribute: value_state
    from: "on"
    to: "off"
    id: "on_to_off"
  - platform: state
    entity_id:
      - binary_sensor.g_eg_vorrat_om_tur_0_g_eg_vorrat_om_tur
    attribute: value_state
    from: "off"
    to: "on"
    id: "off_to_on"
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - "on_to_off" etc...

You don’t even need trigger IDs of you’re just turning on or off another switched to keep things synced:

trigger:
  - platform: state
    entity_id:
      - binary_sensor.g_eg_vorrat_om_tur_0_g_eg_vorrat_om_tur
    attribute: value_state
    from:
      - "on"
      - "off"
    to:
      - "on"
      - "off"
action:
  - service: "switch.turn_{{ trigger.to_state.state }}"
    target:
      entity_id: ...
1 Like

Ah, ok, then I will change my code. Nice to know these trigger ID rules and also that I don’t need the IDs at all here.
Anyway, I want to implement this to get the knowledge and to be able to copy this program to many other light scenarios. This refrigerator light mode I only need 2 times in my house. Most of the other ones are dimming or hue lamps controlled by motion sensors and switches.

Changed my code now to have entitys in the trigger and the actions. But “surprise” doesn’t work. The program doesn’t even show as triggered:

alias: Vorratskammer-Licht-trigger-id-entity
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.g_eg_vorrat_om_tur_0_g_eg_vorrat_om_tur
    attribute: value_state
    from: "on"
    to: "off"
    id: closed
  - platform: state
    entity_id:
      - binary_sensor.g_eg_vorrat_om_tur_0_g_eg_vorrat_om_tur
    attribute: value_state
    from: "off"
    to: "on"
    id: opened
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - closed
        sequence:
          - service: switch.turn_off
            target:
              entity_id: switch.g_eg_vorrat_licht_0_g_eg_vorrat_licht
            data: {}
      - conditions:
          - condition: trigger
            id:
              - opened
        sequence:
          - service: switch.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: switch.g_eg_vorrat_licht_0_g_eg_vorrat_licht
mode: single

This is one of the triggers:

Why there is a second line for the entity? And are the states also translated, do I have to fill them in german language?

remove value state as attribute

:+1: Thanks, now it works:

alias: Vorratskammer-Licht-trigger-id-entity
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.g_eg_vorrat_om_tur_0_g_eg_vorrat_om_tur
    from: "on"
    to: "off"
    id: closed
  - platform: state
    entity_id:
      - binary_sensor.g_eg_vorrat_om_tur_0_g_eg_vorrat_om_tur
    from: "off"
    to: "on"
    id: opened
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - closed
        sequence:
          - service: switch.turn_off
            target:
              entity_id: switch.g_eg_vorrat_licht_0_g_eg_vorrat_licht
            data: {}
      - conditions:
          - condition: trigger
            id:
              - opened
        sequence:
          - service: switch.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: switch.g_eg_vorrat_licht_0_g_eg_vorrat_licht
mode: single

In that case I would say use the code Pieter posted above.
Slightly corrected version of his code:

trigger:
  - platform: state
    entity_id:
      - binary_sensor.g_eg_vorrat_om_tur_0_g_eg_vorrat_om_tur
    from:
      - "on"
      - "off"
    to:
      - "on"
      - "off"
action:
  - service: "switch.turn_{{ trigger.to_state.state }}"
    target:
      entity_id: switch.g_eg_vorrat_licht_0_g_eg_vorrat_licht
1 Like

I don’t know how I could select to options in “from” or “to” at the trigger section. Also I don’t know what to choose at the action section for the switch.turn “trigger to state”.

My program was created with the GUI and I want to avoid “coding” as much as possible. With Homematic I only needed some scripts for speech output with Alexa, but this was only nice to have. Everyting else I could do with the GUI. I am not alone at home and at one point maybe my wife ormy childrens should also be able to maintenence the system.

Another question about this “attribute”. I wanted to use a switch in another program, also as entity at the triggers. But I only get it to run if I remove the attributes and select “from: any status” “to: any status”. But this way I can’t seperate short or long press. As soon as I choose in attribute the “event”, I can select short and long press, but where do I put them, in the “from” or “to” field?. Anyway, I almost tried every combination and none worked, the program doen’t trigger. Only when I remove the attributes.

Any hints?

Oh, and one surprise, when I use the switch as “device” in the trigger, it works fine so far. But yes, I would like to implement it as entity if possible.

Just press the three dots and “Yaml mode” then copy paste all in the code from the code box above