I need help with the following automation. I am taking a hex code pulled in from an RF Bridge and converting it to a decimal and then parsing it out. The digits are as follows id, state, and then battery level. For example 9151088 would be id (915) switch state (1) and battery level (088 or 88%). Everything appears to work in “Template editor” but I am getting no MQTT messages when the automation is triggered. I have a feeling is has something to do with my variables setup but I can’t pin down what I am doing wrong. Any help is greatly appreciated. Thanks.
automation:
- alias: RF Modules Switch and Battery States
mode: restart
trigger:
- platform: state
entity_id: sensor.rf_bridge_received_data_sensor
variables:
# this allows for future models designations
module_id_list: >
{% set string = { '915':'1', '623':'2', '486':'3' } %}
{% set module_id_list = string %}
{{ module_id_list }}
module_id: >
{% set string = ((states('sensor.rf_bridge_received_data_sensor')|int(base=16))|string)[0:3] %}
{% set module_id = string %}
{{ module_id }}
module_id_friendly: >
{% if module_id_list | contains(module_id) %}
{% set string = module_id_list[module_id] %}
{% set module_id_friendly = string %}
{% endif %}
{{ module_id_friendly }}
switch_state: >
{% if module_id_list | contains(module_id) %}
{% set string = ((states('sensor.rf_bridge_received_data_sensor')|int(base=16))|string)[3] %}
{% set switch_state = string %}
{% endif %}
{{ switch_state }}
battery_percent: >
{% if module_id_list | contains(module_id) %}
{% set string = ((states('sensor.rf_bridge_received_data_sensor')|int(base=16))|string)[4:] %}
{% set battery_percent = string %}
{% endif %}
{{ battery_percent }}
action:
- choose:
# IF module is off, safe to pass battery and switch readings
- conditions:
- condition: template
value_template: "{{ switch_state == '1' }}"
sequence:
- service: mqtt.publish
data:
qos: "0"
retain: true
topic: home/rf_devices/module_{{ module_id_friendly }}/state
payload: "off"
- delay: "00:00:01"
- service: mqtt.publish
data:
qos: "0"
retain: true
topic: home/rf_devices/module_{{ module_id_friendly }}/battery
payload: "{{ battery_percent }}"
# ELSE module is on, only pass switch reading
- conditions:
- condition: template
value_template: "{{ switch_state == '0' }}"
sequence:
- service: mqtt.publish
data:
qos: "0"
retain: true
topic: home/rf_devices/module_{{ module_id_friendly }}/state
payload: "on"
I’ve gone through a lot of trial and error and have two observations. I am sure there is a reason for why these will render in the Developer tool but not in practice…just no idea why. As always any help is appreciated. Thanks.
First - The code with not render any of the variables that end in [ ] like below:
dec_value[0:3]
It will render something like this:
{% set module_id =((states(‘sensor.rf_bridge_received_data_sensor’)|int(base=16))|string)[0:3] %}
But not a set variable ending with the [ ]
Second- Statements like this seem to ignore the variable i.e. “module_id” in this example. The statement works if I hardcode it with ‘915’.
module_id_friendly: >
{% if module_id_list | contains(module_id) %}
{% set module_id_friendly = module_id_list[module_id] %}
{% endif %}
{{ module_id_friendly }}
After hours of trial and error I finally have a code that works. For some reason I can’t get the module_id and switch_state variables to play nice so I had to use something like (dec_value|string)[0:3]. Technically I don’t even have to use those variables now since I was forced to use (dec_value|string)[0:3].
If anyone can shed some light on why I had to go this tougher route I would appreciate it for my full understanding. Thanks.