Automation works but YAML is ugly

I’m using demultiplexers to publish topics for various sensors. Here’s one example:

- alias: OMG Pilight Elro demultiplexer
  id: 1a9a1234b34641c09824f583c33fd9f5
  trigger:
    platform: mqtt
    topic: home/OpenMQTTGateway_ESP8266_RF/PilighttoMQTT
  condition:
  - condition: template
    value_template: '{% if trigger.payload_json.protocol==''elro_800_contact'' %} true
      {% endif %}'
  action:
  - service: mqtt.publish
    data_template:
      topic: '{{ ''homeassistant/binary_sensor/'' + trigger.payload_json.protocol|string + trigger.payload_json.message.systemcode|string + trigger.payload_json.message.unitcode|string + ''/state'' }}'
      payload: 'ON'
  - service: mqtt.publish
    data_template:
      topic: '{{ ''homeassistant/binary_sensor/'' + trigger.payload_json.protocol|string + trigger.payload_json.message.systemcode|string + trigger.payload_json.message.unitcode|string + ''/config'' }}'
      payload: '{"name":"{{trigger.payload_json.protocol|string}}{{trigger.payload_json.message.systemcode|string}}{{trigger.payload_json.message.unitcode|string}}", "device_class": "motion", "state_topic": "homeassistant/binary_sensor/{{trigger.payload_json.protocol|string}}{{trigger.payload_json.message.systemcode|string}}{{trigger.payload_json.message.unitcode|string}}/state"}'

How can I make this shorter? Can I assign variables in the condition somehow? Any other code conventions I should be aware of?

Cannot test it but something like this looks a bit better

- alias: OMG Pilight Elro demultiplexer
  id: 1a9a1234b34641c09824f583c33fd9f5
  trigger:
    platform: mqtt
    topic: home/OpenMQTTGateway_ESP8266_RF/PilighttoMQTT
  condition:
    condition: template
    value_template: "{{ trigger.payload_json.protocol=='elro_800_contac'}}"
  action:
  - service: mqtt.publish
    data_template:
      topic: >
        {% set data = trigger.payload_json %}
        {{ 'homeassistant/binary_sensor/{}{}{}/state'.format(data.protocol, data.message.systemcode, data.message.unitcode) }}
      payload: 'ON'
  - service: mqtt.publish
    data_template:
      topic: >
        {% set data = trigger.payload_json %}
        {{ 'homeassistant/binary_sensor/{}{}{}/config'.format(data.protocol, data.message.systemcode, data.message.unitcode) }}
      payload: >
        {% set data = trigger.payload_json %}
        {% set name = data.protocol ~ data.message.systemcode ~data.message.unitcode %}
        {% set state_topic = 'homeassistant/binary_sensor/{}{}{}/state'.format(data.protocol, data.message.systemcode, data.message.unitcode) %}
        {"name":"{{name}}", "device_class": "motion", "state_topic": "state_topic"}

Details - in templating/Jinja docs.
Cannot comment on the algorithm improvements but it looks a bit repetitive… have you seen this?

HA configs are not the prettiest as YAML and its HA implementation is rather restrictive.

1 Like

Rather than explicitly concatenate strings, just let the templating engine do its thing.

- alias: OMG Pilight Elro demultiplexer
  id: 1a9a1234b34641c09824f583c33fd9f5
  trigger:
    platform: mqtt
    topic: home/OpenMQTTGateway_ESP8266_RF/PilighttoMQTT
  condition:
  - condition: template
    value_template: "{{ trigger.payload_json.protocol == 'elro_800_contact' }}"
  action:
  - service: mqtt.publish
    data_template:
      topic: "homeassistant/binary_sensor/{{trigger.payload_json.protocol}}{{trigger.payload_json.message.systemcode}}{{trigger.payload_json.message.unitcode}}/state"
      payload: 'ON'
  - service: mqtt.publish
    data_template:
      topic: "homeassistant/binary_sensor/{{trigger.payload_json.protocol}}{{trigger.payload_json.message.systemcode}}{{trigger.payload_json.message.unitcode}}/config"
      payload: >
        {% set s = trigger.payload_json.protocol ~ trigger.payload_json.message.systemcode ~ trigger.payload_json.message.unitcode %}
        {"name":"{{s}}", "device_class": "motion", "state_topic": "homeassistant/binary_sensor/{{s}}/state"}

I tested the automation by publishing this payload:

{ "protocol":"elro_800_contact", "message":{"systemcode":123456, "unitcode":"ABCXYZ"} }

to this topic:

home/OpenMQTTGateway_ESP8266_RF/PilighttoMQTT

and the automation published this payload:

{"name":"elro_800_contact123456ABCXYZ", "device_class": "motion", "state_topic": "homeassistant/binary_sensor/elro_800_contact123456ABCXYZ/state"}

to this topic:

homeassistant/binary_sensor/elro_800_contact123456ABCXYZ/config

and it published ON to:

homeassistant/binary_sensor/elro_800_contact123456ABCXYZ/state
1 Like

Much better!

@AhmadK I also like your solution will use it for more complex automations.

1 Like