Help with mqtt templating (from Domoticz)

Hi,

As there were some recent activities here, I’ve in the middle time work on some automatic discovery version.

- alias: domoticz_sensor_state_temp
  initial_state: true
  trigger:
     - platform: mqtt
       topic: 'domoticz/out'
  condition:
     condition: template
     value_template: '{{ trigger.payload_json.dtype == "Temp + Humidity" }}'
  action:
     - service: mqtt.publish
       data_template:
         topic: 'homeassistant/sensor/dz_sensor_{{ trigger.payload_json.idx }}/state'
         payload_template: '{{ trigger.payload_json.svalue1 }}'
     - condition: template
       value_template: '{{ states("sensor.dz_sensor_"+trigger.payload_json.idx|string) == "unknown" }}'
     - service: mqtt.publish
       data_template:
         topic: 'homeassistant/sensor/dz_sensor_{{ trigger.payload_json.idx }}/config'
         payload_template: '{"name":"{{ trigger.payload_json.name|string }} (dz)","state_topic":"homeassistant/sensor/dz_sensor_{{ trigger.payload_json.idx|string }}/state","unit_of_measurement":"°C"}'

Code duplicated for sensors with only “Temp” domoticz type, only temperature is reported but you can add this easily.

I’ve the same for switches:

- alias: domoticz_switch_state
  initial_state: true
  trigger:
     - platform: mqtt
       topic: 'domoticz/out'
  condition:
     condition: template
     value_template: '{{ trigger.payload_json.dtype == "Light/Switch" }}'
  action:
     - service: mqtt.publish
       data_template:
         topic: 'homeassistant/switch/dz_switch_{{ trigger.payload_json.idx }}/state'
         payload_template: '{% if trigger.payload_json.nvalue == 1 %} On {% else %} Off {% endif %}'
     - condition: template
       value_template: '{{ states("switch.dz_switch_"+trigger.payload_json.idx|string) == "unknown" }}'
     - service: mqtt.publish
       data_template:
         topic: 'homeassistant/switch/dz_switch_{{ trigger.payload_json.idx }}/config'
         payload_template: '{"name":"{{ trigger.payload_json.name|string }} (dz)","command_topic":"homeassistant/switch/dz_switch_{{ trigger.payload_json.idx|string }}/power","state_topic":"homeassistant/swit
ch/dz_switch_{{ trigger.payload_json.idx|string }}/state","payload_off":"Off","payload_on":"On"}'

- alias: domoticz_switch_power
  initial_state: true
  #hide_entity: false
  trigger:
     - platform: mqtt
       topic: 'homeassistant/switch/+/power'
  action:
     - service: mqtt.publish
       data_template:
         topic: 'domoticz/in'
         payload_template: '{"command": "switchlight", "idx": {{ trigger.topic.split("/")[-2][10:] }}, "switchcmd": "{{ trigger.payload }}" }'

Where you have the back “command” message for HA to command dz switch.

The domoticz device need to send at least one message for the object to appear. I’ve set the domoticz name as HA name with a (dz) suffix, adapt this.

4 Likes