Does anyone know how to go about dynamically specifying the target of a notification at run time? Specifically, I’d like to send notifications to my computer via HTML5 when I’m using it, and to my phone otherwise (currently I come home to all missed notifications suddenly appearing on my computer, despite having seen them on my phone). I have a sensor indicating that I’m at my computer, but the notify target:
doesn’t fall within data:
, so it appears that using data_template:
won’t work.
Have you tried moving it?
My understanding is that all the parts of a service fall under the data bit, but you can bring certain ones out for ease/tidiness.
Actually the issue is that a separate service is registered for each notification device. Target
is then platform specific and depends on the service (e.g. the target browser if multiple html5 endpoints are registered). I gave it a shot those using the notify.notify
service (is this a global service that everyone has or is it a naming issue in my config?):
- alias: Test Dynamic Target Notification
trigger:
- platform: state
entity_id: sensor.front_door_alarm_type
to: '161'
action:
service: notify.notify
data_template:
message: 'Invalid code entered at front door.'
target: >
{% if is_state("binary_sensor.desktop_activity", "on") %}
notify.chrome
{% elif is_state("binary_sensor.desktop_activity", "off") %}
notify.iphone
{% else %}
notify.my_devices #this is a notification group that includes both of the above
{% endif %}
Neither device gets a notification when tested under both states of binary_sensor.desktop_activity
. The logs show the following:
ERROR (Thread-4) [homeassistant.components.notify.ios] The target (['notify.chrome']) does not exist in .ios.conf.
ERROR (Thread-9) [homeassistant.components.notify.ios] The target (['notify.iphone']) does not exist in .ios.conf.
So perhaps notify.notify is an iOS specific service?
Another approach would be to use a conditional statement in a script. But my understanding is that conditionals can only stop a sequence of actions prematurely…what is the cleanest way to implement a MECE conditional action sequence (without having to create multiple “helper” scripts that call one another)?
I think you may have misunderstood the construct of the notify service, which probably wasn’t helped because I thought you were trying to notify to different devices via the html5 platform.
Your case would be…
action:
service_template: >
{% if is_state("binary_sensor.desktop_activity", "on") %} notify.chrome
{% elif is_state("binary_sensor.desktop_activity", "off") %} notify.iphone
{% else %} notify.my_devices {% endif %}
data:
message: 'Invalid code entered at front door.'
Ahh yes, completely forgot about service templates. Thank you - knew there was a simple approach somewhere.