Problems with trigger variable in targets

Now I got you. And now I understand what you talked about :rofl:

In fact the only thing I changed was to delete the data_template.

That may allow it to pass Check Configuration but, upon execution, errors will be reported in Log.

Try the example I posted above or this reduced version:

- alias: co2_alarm
  description: "CO2 - Ampel"
  trigger:
    - platform: state
      entity_id:
        - sensor.netatmo_unser_heim_wohnzimmer_buro_co2
        - sensor.netatmo_unser_heim_wohnzimmer_kuche_co2
        - sensor.netatmo_unser_heim_wohnzimmer_co2
  action:
    - service: input_text.set_value
      target:
        entity_id: >-
          {% set oid = trigger.to_state.object_id %}
          input_text.co2_{{ 'buero' if 'buro' in oid else 'kuche' if 'kueche' in oid else 'wohnzimmer' }}
      data:
        value: >-
          {% set co2 = trigger.to_state.state | int %}
          {% set x = 'grün' if co2 <= 800 else 'gelb' if 800 < co2 <= 1000 else 'orange' if 1000 < co2 <= 1400 else 'rot' %}
          {{ x }} mit {{ co2 }} ppm
  mode: queued
1 Like

As sson as possible I‘ll give it a try. I like short code :sunglasses:

Did you have a chance to try it yet?

Indeed, I tried it this afternoon. It works fine. Thank you for that.
Again I learned how to do it in a better way :+1:

Glad to hear it.

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. It will also place a link below your first post that leads to the solution post. All of this helps users find answers to similar questions. For more information refer to guideline 21 in the FAQ.

1 Like

@123 you used to_state.object_id. Is there a similar solution for entering zones?

Yes, if you are using a Zone Trigger.

Well, of course :rofl:

But what code do I need instead of to_state.object_id?

Is it like this?


  trigger:
    - platform: zone
      entity_id: device_tracker.doris_iphone_2
      zone: zone.home_outer_ring
      event: enter
  action:
    - service: input_datetime.set_datetime
      target:
        entity_id: >-
          {% set enter = trigger.zone.object_id %}
          input_datetime.doris{{ 'outer' if 'outer' in enter }}

Did you try trigger.to_state.object_id?

I didn‘t. I thought it is zone, so I used zone. Sorry for stupid questions but I am an absolute beginner …

By the way, I didn‘t figure out how to test entering a zone by using the dev. tools :frowning:

I tried the following and it didn‘t work. Just the else part is working.

- alias: doris_enter
  trigger:
    - platform: zone
      entity_id: device_tracker.doris_iphone_2
      zone: zone.home_outer_ring
      event: enter
    - platform: zone
      entity_id: device_tracker.doris_iphone_2
      zone: zone.davids_home_middle_ring
      event: enter
    - platform: zone
      entity_id: device_tracker.doris_iphone_2
      zone: zone.home
      event: enter
  action:
    - service: input_datetime.set_datetime
      target:
        entity_id: >-
          {% set oid = trigger.to_state.object_id %}
          input_datetime.doris_{{ 'outer' if 'outer' in oid else 'middle' if 'middle' in oid else 'home' }}
      data:
        datetime: "{{ now().strftime('%Y-%m-%d %H:%M') }}"
  mode: single

I don’t have any device_trackers or zones so I can’t test anything for you. In this situation you have three separate triggers so it’s more efficient to simply assign an id to each trigger and use that in the template.

- alias: doris_enter
  trigger:
    - platform: zone
      entity_id: device_tracker.doris_iphone_2
      zone: zone.home_outer_ring
      event: enter
      id: 'outer'
    - platform: zone
      entity_id: device_tracker.doris_iphone_2
      zone: zone.davids_home_middle_ring
      event: enter
      id: 'middle'
    - platform: zone
      entity_id: device_tracker.doris_iphone_2
      zone: zone.home
      event: enter
      id: 'home'
  action:
    - service: input_datetime.set_datetime
      target:
        entity_id: "input_datetime.doris_{{ trigger.id }}"
      data:
        datetime: "{{ now().timestamp() | timestamp_local }}"
  mode: single

Boah, that easy. Thank you so much!!!
But what is wrong with my datetime?

I posted a different version of the datetime template just to show there’s another way to do the same thing.

I tried the code but unfortunately it doesn‘t work at all. None of the timedate inputs are filled :frowning:

How did you test the automation? Did device_tracker.doris_iphone_2 move from one zone to another?

Yep. In real life :open_mouth: