Problems with trigger variable in targets

I have the following code and if I use a single target it works fine but the target seems not to work with the trigger variables.

- 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: input_text.co2_buero
      data_template:
        target: >-
          {% set ent = trigger.entity_id %}
          {% if ent == 'sensor.netatmo_unser_heim_wohnzimmer_buro_co2' %}
            entity_id: input_text.co2_buero
          {% elif ent == 'sensor.netatmo_unser_heim_wohnzimmer_kuche_co2' %}
            entity_id: input_text.co2_kueche
          {% elif ent == 'sensor.netatmo_unser_heim_wohnzimmer_co2' %}
            entity_id: input_text.co2_wohnzimmer
          {% endif %}
      data:
        value: >-
          {% if states(trigger.entity_id)|int <= 800 %}
            grün mit {{ states(trigger.entity_id)|int }} ppm
          {% elif states(trigger.entity_id)|int > 800 and
            states(trigger.entity_id)|int <= 1000 %}
            gelb mit {{ states(trigger.entity_id)|int }} ppm
          {% elif states(trigger.entity_id)|int > 1000 and
            states(trigger.entity_id)|int <= 1400 %}
            orange mit {{ states(trigger.entity_id)|int }} ppm
          {% elif states(trigger.entity_id)|int > 1400 %}
            rot mit {{ states(trigger.entity_id)|int }} ppm
          {% endif %}
  mode: queued

Could anybody help please?

You use a template to produce a value that will be assigned to an option.

some_option: "{{ template to produce a value for "some_option" }}"

You cannot use a template to produce options.

data: "{{ template producing more options and values }}"

Try this 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 ent = trigger.entity_id %}
          {% if ent == 'sensor.netatmo_unser_heim_wohnzimmer_buro_co2' %}
            input_text.co2_buero
          {% elif ent == 'sensor.netatmo_unser_heim_wohnzimmer_kuche_co2' %}
            input_text.co2_kueche
          {% elif ent == 'sensor.netatmo_unser_heim_wohnzimmer_co2' %}
            input_text.co2_wohnzimmer
          {% endif %}
      data:
        value: >-
          {% set co2 = trigger.to_state.state | int %}
          {% if co2 <= 800 %} grün mit {{ co2 }} ppm
          {% elif 800 < co2 <= 1000 %} gelb mit {{ co2 }} ppm
          {% elif 1000 < co2 <= 1400 %} orange mit {{ co2 }} ppm
          {% else %} rot mit {{ co2 }} ppm
          {% endif %}
  mode: queued

If you wish, you can replace all of this:

        entity_id: >-
          {% set ent = trigger.entity_id %}
          {% if ent == 'sensor.netatmo_unser_heim_wohnzimmer_buro_co2' %}
            input_text.co2_buero
          {% elif ent == 'sensor.netatmo_unser_heim_wohnzimmer_kuche_co2' %}
            input_text.co2_kueche
          {% elif ent == 'sensor.netatmo_unser_heim_wohnzimmer_co2' %}
            input_text.co2_wohnzimmer
          {% endif %}

with this:

        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' }}

Sorry but I really don’t know what you are talking about. I do not produce more options and/or values. How did you come to this? Please be more precise.

Anyway, I figured out I had a typo and the code is running now. That’s why I really want to understand what you are talking about.

Wonderful, post the corrected version so we can all see that one typo you claimed it had.

You can’t template target and you can’t declare data_template and data under target because it counts as two instances of the same option (because data_template is the equivalent of data).

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?