Sorry for the bump, but I wanted to see if I can get some help to extend this cool feature.
The example @arsaboo posted with the initial bits plus the final part works great, but it sends notifications for all users all the time.
I can add if conditions to the action to try to filter for a single user, but since trigger is met, the message fires all the time. They work, but it too late. The automation has fired and notifies regardless of what the if in the message. Which makes sense.
I just want to get an alert when my son is watching something, not everyone else in the house.
I was hoping for something like this added into a condition would work.
It doesn’t trigger an HA error on load, nor does it work:
- alias: "The Kid is Watching Plex Notice"
trigger:
platform: state
entity_id: sensor.plex
condition:
condition: template
value_template: '{{states.sensor.plex.attributes.johnnyboy}}'
action:
service: notify.pushbullet
data_template:
message: >
{% for attr in states.sensor.plex.attributes %}
{%- if not attr=="friendly_name" and not attr=="unit_of_measurement" and not attr=="icon" -%}
{{attr}} is watching: {{states.sensor.plex.attributes[attr]}}
{%- endif %}
{%- endfor -%}
title: Check Johnnyboy's Show
So in this case, johnnyboy in the value_template’s condition would be the username that the Plex sensor reports.
I would love the trigger to only go off when the attribute johnnyboy exists.
If it exists, do the action’s notification. If not, do nothing.
I based the {{states.sensor.plex.attributes.johnnyboy}} in the condition’s template off of examples I’ve seen regarding attributes like a lights brightness existing or not, but this just isn’t cutting it. I’ve tried so many things at this point based on a bunch of other examples I’ve read, but in the majority of those the testing is done against a known value equaling a number value or string.
I’ve tried a few combos of
{%- if states.sensor.plex.attributes[0] == “johnnyboy” -%}
…
or [1],etc. to try to cover array possibilities
and…
{%- if states.sensor.plex.attributes == “johnnyboy” -%}
…
… quoted, non-quoted, etc…
and…
{%- if states.sensor.plex.attributes.johnnyboy = true -%}
… (or True or TRUE or not null or NOT NULL or is string ,etc. ) … quoted, non-quoted etc…
… but I get all sorts of errors in any and all attempts when the automations load using these types of tests.
Perhaps I am just missing the syntax on some of the possibilities that would work. I am just starting to figure out the jinja templating stuff for HA, so I fear that may be the case.
So, a long way to get here…
Is there a way to test for the existence of an attribute’s key name in a condition or trigger directly?
Please tell me I am just missing something simple or nor understanding the test conditions properly.
Thanks in advance!!!
cheers!
J