RF Automation and Variables

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 don’t think you can template the topic

I took the templates out of the topics and it is still not publishing.

    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_1/state
                 payload: "off"
             - delay: "00:00:01"
             - service: mqtt.publish
               data:
                 qos: "0"
                 retain: true
                 topic: home/rf_devices/module_1/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_1/state
                 payload: "on"

Here you need payload_template:

Made this change and it still is not publishing. BTW - I do appreciate your help.

What does the trace say ?

There is nothing wrong with the publish itself

afbeelding

afbeelding

I ran the trace and the choose is not working.

I dove a little deeper on the trace. It looks like the last three variables are not being set and this is why the choose is not working.

trigger:
  id: '0'
  idx: '0'
  alias: null
  platform: state
  entity_id: sensor.rf_bridge_received_data_sensor
  from_state:
    entity_id: sensor.rf_bridge_received_data_sensor
    state: 9B1964
    attributes:
      friendly_name: RF Bridge Received Data Sensor
    last_changed: '2024-02-22T15:59:34.855250+00:00'
    last_updated: '2024-02-22T15:59:34.855250+00:00'
    context:
      id: 01HQ8RPAE7SQMHFTEXJKWCJ9NA
      parent_id: null
      user_id: null
  to_state:
    entity_id: sensor.rf_bridge_received_data_sensor
    state: 8B9E84
    attributes:
      friendly_name: RF Bridge Received Data Sensor
    last_changed: '2024-02-22T15:59:37.307891+00:00'
    last_updated: '2024-02-22T15:59:37.307891+00:00'
    context:
      id: 01HQ8RPCTVJ7N9FMXKK974Q5BM
      parent_id: null
      user_id: null
  for: null
  attribute: null
  description: state of sensor.rf_bridge_received_data_sensor
module_id_list:
  '486': '3'
  '623': '2'
  '915': '1'
module_id: 915
module_id_friendly: ''
switch_state: ''
battery_percent: ''

Those three variables use an “if” statement so I must have written them wrong. Any idea on how to correctly write them?

I don’t get 915 Sorry, need 8B9E84

You need to convert your module_id_list to json :

{% set rf_bridge_received_data_sensor = '8B9E84' %}
                     {% set string = { '915':'1', '623':'2', '486':'3' } %}
                      {% set module_id_list = string %}
                      {{ module_id_list | to_json }}
                      
{% set string = ((rf_bridge_received_data_sensor|int(base=16))|string)[0:3] %}
              {% set module_id = string %}
                module_id {{ module_id }}
                      
      module_id_friendly: >
                          {% if module_id_list | contains(module_id) %}
                          {{ true }}
                             {% set string = module_id_list[module_id] %}
                             {% set module_id_friendly = string %}
                          {% endif %}
                          {{ module_id_friendly }}

I have not tried your changes yet but it does render with my current method. Just doesn’t work in actual practice :frowning: .

Just try with the | to_json and see what it gives.

Tried it, it renders in tools but still does not work in practice. Again checked the tracer and it’s those “if” statements. They just don’t populate.

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 }}
    variables:
      dec_value: >
                 {% set dec_value = (states('sensor.rf_bridge_received_data_sensor')|int(base=16))|string %}
                 {{ dec_value }}
      
      module_id_list: >
                      {% set module_id_list = { '915':'1', '623':'2', '486':'3' } %}
                      {{ module_id_list }}
      module_id: >
                 {% set module_id = dec_value[0:3] %}
                 {{ module_id }}
      module_id_friendly: >
                          {% if module_id_list | contains(module_id) %}
                             {% set module_id_friendly = module_id_list[module_id] %}
                          {% endif %}
                          {{ module_id_friendly }}
      switch_state: >
                    {% if module_id_list | contains(module_id) %}
                       {% set switch_state = dec_value[3] %}
                    {% endif %}
                    {{ switch_state }}
      battery_percent: >
                       {% if module_id_list | contains(module_id) %}
                          {% set battery_percent = dec_value[4:] %}
                       {% endif %}
                       {{ battery_percent }}

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.

automation:
  - alias: RF Modules Switch and Battery States
    id: "060708"
    mode: restart
    trigger:
      - platform: state
        entity_id: sensor.rf_bridge_received_data_sensor
    variables:
      dec_value: >
                 {% set dec_value = (states('sensor.rf_bridge_received_data_sensor')|int(base=16))|string %}
                 {{ dec_value }}
      
      module_id_list: >
                      {% set module_id_list = { '915':'1', '623':'2', '486':'3' } %}
                      {{ module_id_list }}
      module_id: >
                 {% set module_id = (dec_value|string)[0:3] %}
                 {{ module_id }}
      module_id_friendly: >
                          {% if module_id_list | contains((dec_value|string)[0:3]) %}
                             {% set module_id_friendly = module_id_list[(dec_value|string)[0:3]] %}
                          {% endif %}
                          {{ module_id_friendly }}
      switch_state: >
                    {% if module_id_list | contains((dec_value|string)[0:3]) %}
                       {% set switch_state = (dec_value|string)[3] %}
                    {% endif %}
                    {{ switch_state }}
      battery_percent: >
                       {% if module_id_list | contains((dec_value|string)[0:3]) %}
                          {% set battery_percent = (dec_value|string)[4:] %}
                       {% endif %}
                       {{ battery_percent }}
    action:
     - choose:          
         # IF module is off, safe to pass battery and switch readings
         - conditions:
             - condition: template
               value_template: "{{ (dec_value|string)[3] == '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: "{{ (dec_value|string)[3] == '0' }}"
           sequence:
             - service: mqtt.publish
               data:
                 qos: "0"
                 retain: true
                 topic: home/rf_devices/module_{{ module_id_friendly }}/state
                 payload: "on"