Trying to create a new automation that triggers based on the state value of a nested attribute, but not finding a solution. I see how to trigger on an attribute, but not a nested attribute.
Below are some of the attribute contents from the Garmin integration, and sensor sensor.last_activity:
I’d like to trigger the automation on the nested attribute activityType: typeKey:, so when typeKey changes to a known activity, the automation would fire. The attribute activityName: is user defined, whereas typeKey: is integration defined, I’d rather trigger off the integration defined name than a user degined name.
I’ve gotten this far, but I’m not sure how to get the nested attribute typeKey: state?
trigger: state
entity_id:
- sensor.last_activity
attribute: activityType
So create a new template sensor that extracts the sub-attribute, and stores it as a state, then trigger off that new template sensor? Would the new template sensor need it’s own automation to check if the sub-attribute has changed?
Trying to build that template sensor to extract the nested data, but I don’t think I’m understanding how to get to the nested attribute; this is what I’ve got but it’s not working, looks like state_attr is only accepting 3 positional arguments, not 4.
Hmm, using the above creates values that are quoted, and I’m not sure why…
e.g.
So a state trigger for hiking isn’t the same as "hiking", and setting the trigger state for "hiking" isn’t working. I’m thinking stripping the "" would fix the issue, just not sure how to-do that?
edit: I believe the template needs to be "quoted" in order to function properly…
Can you paste the YAML you’re using for the sensor? I think you probably need to remove the quotes, but that’s only if you’ve got something like this in the YAML:
So I built a template sensor to extract the nested value I’m trying to trigger off of, that template looks like this (and produces the "quoted" string):
Just note that the values recorded previously in the history will still have the quotes. But once you fix it the sensor won’t put the quotes in moving forward.
The quotes are a pain. If you use the automation editor, don’t use them, the automation editor will do it. If you use yaml, use them when it is a single line, not when you use > or | to put the template on sepatate line(s).
I think this is also a side effect of using Home Assistant for almost a decade, there was no UI editor early on, and I code regularly outside of the HA project, so I tend to follow normal coding standards. So the UI does things that aren’t expected, like adding " around things for you. Guess I’m just getting old, and miss the manual way of doing things.