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?
{% 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
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') %}
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 ~ '%'
}}