Is `trigger` variable supposed to be accessible in jinja2 templating?

Hi everyone!

Newbie here. I try to make the automation send notifications based on trigger info.
However, when I try using data_template in conjunction with jinja2 and trigger variable, the automation seems badly broken, that the homeassistant would not even restart.

- id: bathroom_humidity_low
  alias: Turn off Towel Heater when Bathroom Humidity Low or nobody is home
  hide_entity: true
  trigger:
  - platform: numeric_state
    entity_id: sensor.bathroom_humidity
    below: 80
    #below: 55
  - platform: state
    entity_id: group.family
    from: 'home'
    to: 'not_home'
    for: "00:15:00"
  condition:
    condition: state
    entity_id: switch.bathroom_towel_heater_plug
    state: 'on'
  action:
  - service: switch.turn_off
    entity_id: switch.bathroom_towel_heater_plug
  - service: notify.telegram_group
    data_template:
      title: "Bathroom Humidity is *{{ states.sensor.bathroom_humidity.state }}*"
      message: >
        {% if is_state('trigger.to_state', 'not_home') %}
          "Turning the Bathroom Towel Heater Off because nobody is home"
        {% else %}
          "Turning the Bathroom Towel Heater Off because the bathroom humidity is below 80%"
        {% endif %}

I wonder if it is because of is_state('trigger.to_state', 'not_home') is not available in jinja2 condition, or may be other reasons why the automation breaks the config?

write that as:

{% if trigger.to_state.state == 'not_home' %}
          "Turning the Bathroom Towel Heater Off because nobody is home"
{% else %}
          "Turning the Bathroom Towel Heater Off because the bathroom humidity is below 80%"
{% endif %}

though I am not sure if the 2 possible trigger platforms wouldn’t be confusing the template…they are not mutually exclusive

It shouldn’t break, but it won’t work.

The issue is with this line:

        {% if is_state('trigger.to_state', 'not_home') %}

Your first issue is that you are wrapping the trigger object call in quotes. This actually doesn’t make the template access the trigger object. Instead you just made a string.

Second, if you are trying to use the is_state method, you need to provide an entity_id to it, not a state object. trigger.to_state without quotes is a state object. Passing that to is_state will do nothing but error.

So with that being said, the valid way to do this is:

        {% if is_state(trigger.entity_id, 'not_home') %}

or

        {% if is_state(trigger.to_state.entity_id, 'not_home') %}

or

        {% if trigger.to_state.state == 'not_home' %}
1 Like

Pretty much agree with everything that has been said, especially:

Given the way the triggers are written, I would suggest this:

      message: >
        {% if trigger.platform == 'state' %}
          "Turning the Bathroom Towel Heater Off because nobody is home"
        {% else %}
          "Turning the Bathroom Towel Heater Off because the bathroom humidity is below 80%"
        {% endif %}

EDIT: And, actually, you don’t need the quotes because you’re using multi-line YAML. So I’d actually suggest:

      message: >
        Turning the Bathroom Towel Heater Off because {{
          'nobody is home' if trigger.platform == 'state' else
          'the bathroom humidity is below ' ~ trigger.below ~ '%'
        }}
3 Likes

yes, very nice indeed!

so nice to see the trigger.below variant for once, that must be a heavily overlooked option, never see it fly by.

@Mariusthvdb and @pnbruckner Thank you again!