I’m trying to figure out a not-as-repetitive way to set up persistent notifications when one of 433MHz door/window sensor sends a low battery message. Any of the sensor (apparantly–I haven’t tested this) use the same “end-of-string” (‘06’) in the ‘Data’ component of the full JSON string.
That is, read from topic tele/sonoff_rfb_01/RESULT the payload for RfReceived.Data could be any of D08806, D29406, D04206, D04D06 etc.)
As I already have 5 of these sensors, and plan to have ~20, I’d like to try a less repetitive method of setting up persistent notification for the low battery warning, that is fairly easy to add to as I add sensors. I’d have though some kind of wildcards would be best for this, but can’t seem to find if there are any in HA templates, but seems like .endswith could be used.
My initial attempt is as follows:
#Persistent notifications (in UI)
- alias: Door/window sensor batteries
trigger:
platform: mqtt
topic: tele/sonoff_rfb_01/RESULT
condition:
condition: template
value_template: '{{trigger.payload_json.RfReceived.Data.endswith('06')}}'
action:
service: persistent_notification.dismiss
data:
title: "Sensor battery is low in the..."
data_template:
message: >-
{% set sensor_batteries = {'D08806':'upstairs rear patio security door', 'D29406':'upstairs rear patio glass door', 'D04206':'rear laundry security door', 'D04D06':'bedroom patio security door'} %}
{% for i in sensor_batteries %}
{if i in trigger.payload %} {{sensor_batteries[i]}} {% endif %}
{% endfor %}
However, on testing with the Configurator, I get and related errors. Does anyone have tips or spots to read up on potential options to pursue?
So… I now have this working using "06" in trigger.payload_json.RfReceived.Data (and a few other tweaks/fixes), but would prefer to use something that specifies that “06” must be at the end to avoid potential issues if future codes contain “06” elsewhere.
#Persistent notifications (in UI)
- alias: Door/window sensor batteries
trigger:
platform: mqtt
topic: tele/sonoff_rfb_01/RESULT
condition:
condition: template
value_template: '{{ "06" in trigger.payload_json.RfReceived.Data }}'
action:
service: persistent_notification.create
data:
title: "Sensor battery is low for the..."
data_template:
message: >-
{% set sensor_batteries = {'D08806':'...upstairs rear patio security door.', 'D29406':'...upstairs rear patio glass door.', 'D04206':'...rear laundry security door.', 'D04D06':'...bedroom patio security door.'} %}
{% for i in sensor_batteries %}
{% if i in trigger.payload %} {{sensor_batteries[i]}} {% endif %}
{% endfor %}
Notice how the entire template is delimited by double-quotes whereas as the 06 is delimited by single-quotes. This works.
It won’t work if you changed the example and used single-quotes to delimit the entire template (as you had in your original example). If you do that then are you obliged to change the 06 to use double-quotes.
Use one type of quotes for delimiting the entire template, use the other type for delimiting strings within the template.