Please help with trigger.entity_id in conditions

Hello everybody,

I have an automation, that should notify me on occuring errors on my vacuum bots. I have plenty of coding skills especially .net, but yaml and jinja is still relatively new for me.

alias: Benachrichtigung Staubsauger
description: ""
triggers:
  - trigger: state
    entity_id:
      - sensor.ufo_fehler
      - sensor.ouzo_fehler
    from: "0"
    not_to:
      - unavailable
      - unknown
conditions: []
actions:
  - choose:
      - conditions:
          - condition: state
            entity_id: trigger.entity_id
            state: "103"
        sequence:
          - metadata: {}
            data:
              title: Saugroboter braucht Hilfe!
              message: >-
                Hallo Heike, {% if trigger.to_state.entity_id ==
                "sensor.ouzo_fehler" %} Ouzo {% else %} Yiko {% endif %} muss
                gerettet werden!"
            action: notify.mobile_app_sm_m225fv
      - conditions:
          - condition: state
            entity_id: trigger.entity_id
            state: "301"
        sequence:
          - metadata: {}
            data:
              title: Saugroboter braucht Hilfe!
              message: >-
                Hallo Heike, {% if trigger.to_state.entity_id ==
                "sensor.ouzo_fehler" %} Ouzo {% else %} Yiko {% endif %} hat
                Durst!"
            action: notify.mobile_app_sm_m225fv
      - conditions:
          - condition: state
            entity_id: trigger.entity_id
            state: "302"
        sequence:
          - metadata: {}
            data:
              title: Saugroboter braucht Hilfe!
              message: >-
                Hallo Heike, {% if trigger.to_state.entity_id ==
                "sensor.ouzo_fehler" %} Ouzo {% else %} Yiko {% endif %} hat
                Pipi gemacht!"
            action: notify.mobile_app_sm_m225fv
    default:
      - metadata: {}
        data:
          title: Saugroboter ist krank!
          message: >-
            Hallo Heike, mit {% if trigger.to_state.entity_id ==
            "sensor.ouzo_fehler" %} Ouzo {% else %} Yiko {% endif %} stimmt
            irgendwas nicht!"
        action: notify.mobile_app_sm_m225fv
mode: single

Something with the condition is not working how I imagine it would, because all I get is the default path of the choose block.

The trace for the latest trigger looks like this:

this:
  entity_id: automation.benachrichtigung_ufo
  state: 'on'
  attributes:
    id: '1713989481918'
    last_triggered: '2024-12-27T11:32:12.106134+00:00'
    mode: single
    current: 0
    friendly_name: Benachrichtigung Staubsauger
  last_changed: '2024-12-30T00:38:55.862851+00:00'
  last_reported: '2024-12-30T00:38:55.862851+00:00'
  last_updated: '2024-12-30T00:38:55.862851+00:00'
  context:
    id: 01JGAG0T3PH4KZBMR5D7B9VW0G
    parent_id: null
    user_id: null
trigger:
  id: '0'
  idx: '0'
  alias: null
  platform: state
  entity_id: sensor.ufo_fehler
  from_state:
    entity_id: sensor.ufo_fehler
    state: '0'
    attributes:
      description: 'NoError: Robot is operational'
      friendly_name: Yiko Fehler
    last_changed: '2024-12-30T00:38:36.668072+00:00'
    last_reported: '2024-12-30T00:38:36.668072+00:00'
    last_updated: '2024-12-30T00:38:36.668072+00:00'
    context:
      id: 01JGAG07BWMRXJ66N8MBNMPMVW
      parent_id: null
      user_id: null
  to_state:
    entity_id: sensor.ufo_fehler
    state: '302'
    attributes:
      description: WasteWaterBox full
      friendly_name: Yiko Fehler
    last_changed: '2024-12-30T12:11:34.359721+00:00'
    last_reported: '2024-12-30T12:11:34.359721+00:00'
    last_updated: '2024-12-30T12:11:34.359721+00:00'
    context:
      id: 01JGBQN2JQEM533X2MNAD6NWT3
      parent_id: null
      user_id: null
  for: null
  attribute: null
  description: state of sensor.ufo_fehler

Thanks in advance for pointing me in the correct direction!

I am not sure you can reference a state like that in conditions.

If it were me I would try and use a template condition, something like this maybe:

conditions:
  - condition: template
    value_template: ā€œ{{ trigger.to_state.state == ā€˜103ā€™ }}

Hi bigbadoooff,

Trigger.entity_id is a variable created in the this. stuff. Put it in "{{ }}" when you use it.
Because of that, you need a template condition there if you want to reference it.

thanks, I will try this. Itā€™s gonna be a few days until the trigger occurs again but I will come back to you.

The documentation on the state trigger looked to me as if the trigger.entity_id should be available here. I donā€™t understand completly where templates are allowed and where not.

After some additional reading, please correct me if Iā€™m wrong: trigger.entity_id is shorthand for this.trigger.entity_id and therefore references the current conditional check and not the initial trigger. But why should it then be available again when using a template condition? If it is, I canā€™t find the correct syntax.

Where exactly did you read that?

(Because itā€™s wrong)

The reason why the following line is wrong is because the string trigger.entity is literally not an entity_id.

Itā€™s a variable and can only be used in a template.

The variable trigger is an object that contains details about which trigger triggered the automation.

Reference

Available trigger data

The limitation here is that a State Condition (what you used in your example) doesnā€™t support templates. So thatā€™s why rossk suggested you use a Template Condition.

Not quite correct, look at the trace you posted above and pay attention to the indentation. You see the ā€œthisā€ section, there is nothing in that section that is of any use to you based on this automation.

The information you want to reference in your use case can all be found in the ā€œtriggerā€ section.

So if you want to reference something under trigger you first need,

trigger.

Followed by the next indentation, trigger_from or trigger_to for example.

trigger.from_state
Or
trigger.to_state

From there you can now reference something under from_state or to_state

trigger.from_state.state
Or
trigger_from_state.entity_id

You can reference anything you like from the above trace and can pretty much use yaml anywhere.

Hope that makes sense as itā€™s getting late here and I go just be rambling at this point

If youā€™re interested, hereā€™s a way to to create your automation without the use of a choose statement.

It uses script variables to determine which title and message should be displayed based on which sensor triggered the automation and its state value. Only a single notify action is needed.

alias: Benachrichtigung Staubsauger
description: ""
triggers:
  - trigger: state
    entity_id:
      - sensor.ufo_fehler
      - sensor.ouzo_fehler
    from: "0"
    not_to:
      - unavailable
      - unknown
conditions: []
actions:
  - variables:
      name: "{{ iif('ouzo' in trigger.entity_id, 'Ouzo', 'Yiko') }}"
      fehler:
        '103': 'muss gerettet werden'
        '301': 'hat Durst'
        '302': 'hat Pipi gemacht'
      msg: "{{ fehler.get(trigger.to_state.state, 'stimmt irgendwas nicht') }}"
      titel: "{{ iif(trigger.to_state.state in fehler.keys(), 'braucht Hilfe', 'ist krank') }}" 
  - action: notify.mobile_app_sm_m225fv
    metadata: {}
    data:
      title: "Saugroboter {{ titel }}!"
      message: "Hallo Heike, {{ name }} {{ msg }}!"
2 Likes

[quote=ā€œSir_Goodenough, post:3, topic:819164ā€]
Trigger.entity_id is a variable created in the this. stuff.
[/quote] I misunderstood that sentence.
ā€œthisā€ is the state of the automation on triggering, thanks for clarification

I will have a look at this automation. Thanks for your help!

1 Like