Help with mqtt templating (from Domoticz)

Thanks to this thread, I’ve been able to link domoticz temp sensors, switches and thermostat with home assistant with the help of this thread. I’m used a more generic approach, if this can help.

For temperature sensors, I’ve an automation which rewrote domoticz/out to some "/home/…idx…/temp topic like this:

- alias: DZ temp
  trigger:
     - platform: mqtt
       topic: 'domoticz/out'
  condition: 
     condition: template
     value_template: '{{ trigger.payload_json.dtype == "Temp" }}'
  action:
     - service: mqtt.publish
       data_template:
         topic: 'home/sensor/{{ trigger.payload_json.idx }}/temp'
         payload_template: '{{ trigger.payload_json.svalue1 }}'

I’ve another similar automation for Temp & Humidity.

After you can create an ha sensor like this:

  - platform: mqtt
    state_topic: 'home/sensor/123/temp'   ...

I’ve make the same for switches, for switches I’ve also an automation to rewrote messages to domoticz/in. A dz switch is configured like this:

  - platform: mqtt
    name: "..."
    command_topic: "home/switch/12345/power"
    state_topic: "home/switch/12345/state"
    payload_on: "On"
    payload_off: "Off"
    qos: 1
    retain: true

A first script read domoticz/out to rewrote states from dz:

- alias: DZ input
  trigger:
     - platform: mqtt
       topic: 'domoticz/out'
  condition: 
     condition: template
     value_template: '{{ trigger.payload_json.dtype == "Light/Switch" }}'
  action:
     - service: mqtt.publish
       data_template:
         topic: 'home/switch/{{ trigger.payload_json.idx }}/state'
         payload_template: '{% if trigger.payload_json.nvalue == 1 %} On {% else %} Off {% endif %}'

And a second one rewrote power topic to domoticz/in topic and expected payload:

- alias: DZ switch send value as dz expect
  trigger:
     - platform: mqtt
       topic: 'home/switch/+/power'
  action:
     - service: mqtt.publish
       data_template:
         topic: 'domoticz/in'
         payload_template: '{"command": "switchlight", "idx": {{ trigger.topic.split("/")[-2] }}, "switchcmd": "{{ trigger.payload }}" }'

Same principle for thermostat

climate:
  - platform: mqtt
    name: Thermostat confort
    temperature_command_topic: 'home/thermostat/18/target'
    temperature_state_topic: 'home/thermostat/18/state'

And:

- alias: thermostat get value
  trigger:
     - platform: mqtt
       topic: 'domoticz/out'
  condition: 
     condition: template
     value_template: '{{ trigger.payload_json.dtype == "Thermostat" }}'
  action:
     - service: mqtt.publish
       data_template:
         topic: 'home/thermostat/{{ trigger.payload_json.idx }}/state'
         payload_template: '{{ trigger.payload_json.svalue1 }}'
                            
- alias: dz resend values
  trigger:
     - platform: mqtt
       topic: 'home/thermostat/+/target'
  action:
     - service: mqtt.publish
       data_template:
         topic: 'domoticz/in'
         payload_template: '{"command": "udevice", "idx": {{ trigger.topic.split("/")[-2] }}, "svalue": "{{ trigger.payload }}"}'

I think this principle can be used for other devices type.

Your domoticz managed devices can be imported in your ha installation. As I’ve an old but fully stable domoticz setup working, this is really nice for me to be able to get those devices in ha.