I’ve been playing around with HA for about a month now. Coming from SmartThings, I’m very impressed with the amount of customizability. The documentation and community support has been very helpful, but unfortunately I’ve hit a wall with this problem:
I agree with @anon43302295 on this. It’s a pain in the ass trying to figure out whats inside trigger attributes. It depends on where it’s coming from. For safety, I always get the entity_id out and work with that when dealing with unknown to_state attributes:
example:
...
action:
- service: notify.telegram_group
data_template: #@pnbruckner's change, which is required.
message: >
{% set state = states(trigger.entity_id) %}
{{ state.name }} is {{ state.state }}
I’m not sure this would work. Doesn’t states() return the state as a string instead of the state object? Wouldn’t it need to be something like the following?
{% set e = trigger.entity_id %}
{{ state_attr(e, 'friendly_name') }} is {{ states(e) }}
Personally I have no problem using the trigger variable. In this case it’s pretty clear that it is from a state trigger. And even when there are different types of triggers, there’s always a way to test which one happened. And sometimes it’s important to use the trigger variable, because it shows exactly the state change (or time or whatever) that caused the trigger, when by the time the entity is being tested (in a condition or in the action sequence) it’s possible (although usually unlikely) that the entity has changed. But, I guess this comes down to personal preference mostly.
ah yes, I was thinking of the selectattr method: This is what I usually use. I should have actually looked at my code.
...
action:
- service: notify.telegram_group
data_template: #@pnbruckner's change, which is required.
message: >
{% set state = states | selectattr('entity_id', 'eq', trigger.entity_id) %}
{{ state.name }} is {{ state.state }}
That is weird. Must have something to do with the particular notification platform you’re using. Is there more to that error message that might help?
In any case, if you only care about home vs away (and not what zone they might have entered), you could maybe try:
message: >
{{ trigger.to_state.name }} is {{ 'home' if trigger.to_state.state == 'home' else 'away' }}
In fact, if you don’t care about zone transitions (other than home) you might even want to add a condition that trigger.from_state.state or trigger.to_state.state is ‘home’ so that you don’t get notifications if someone transitions between not_home and a zone or vice versa. Something like: