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.

7 Likes

Hi, is it possible share how did you do it with node-red?

@rimram31 _ I am trying to retrieve temp information from domoticz. I was wondering how to select the correct device ? Do I have to change the txt “idx” with the “domoticz-idx” ?

Edit: I now understand that your script retrieves all sensor temp information. Sorry for asking
_

@curious ops, sorry to answer very late …

Look at my code, the sensor part. The trigger is generic, it convert dz mqtt activity to more “standard” mqtt messages, whatever the domoticz idx is. So a temperature change is published as home/sensor//temp for all temperature sensors you have.

After you need to define a ha mqtt platrofm sensor for every sensor you want to have in home assistant and here explicitely give the domoticz idx.

- platform; mqtt
  state_topic: 'home/sensor/123456/temp'
  ...

Where 123456 is the dz idx.

On this subject, I need to test again but it’s no more working on my installation, I need to check (it seems trigger are no more evaluated).

Hi Did you get your trigger to work again since I tried your temp sensors script but it isn’t triggering.

Hi,

Have you debug domoticz mqtt messages? The automation I’ve push depend on the sensor type, I’ve both a “Temp” rule and a “Temp + Humidity” rule depending on sensor type, as 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 }}'
                
- alias: DZ temp+humidity
  trigger:
     - platform: mqtt
       topic: 'domoticz/out'
  condition: 
     condition: template
     value_template: '{{ trigger.payload_json.dtype == "Temp + Humidity" }}'
  action:
     - service: mqtt.publish
       data_template:
         topic: 'home/sensor/{{ trigger.payload_json.idx }}/temp'
         payload_template: '{{ trigger.payload_json.svalue1 }}'
     - service: mqtt.publish
       data_template:
         topic: 'home/sensor/{{ trigger.payload_json.idx }}/hum'
         payload_template: '{{ trigger.payload_json.svalue2 }}'

And this:

sensor:
  - platform: mqtt
    state_topic: 'home/sensor/123456/temp'
    name: '...'
    unit_of_measurement: '°C'

Easier if you display mqtt messages (with mosquitto client for example).

sorry I found out very late yesterday that I had to turn the automation on in the gui. now it works perfectly!

this is really cool! I like it, so easy, I wasn’t thinking about that.
perfekt, thank you

I have still error after running automation:
ERROR (MainThread) [homeassistant.helpers.service] Error rendering data template: UndefinedError: ‘trigger’ is undefined

Been trying few days to integrate Domoticz in HA, feels Im almost there :slight_smile: I need to run Domoticz aside HA for my switches for now. Your mqtt script works partially for my switches, when switching on the switch goes off in HA again but the light stays on. Any hint for this?

Thanks for all the help, now it works.
Special thanks to @rimram31

Change 55 or 281 to your idx

Config file

sensor:
  - platform: mqtt
    state_topic: 'home/sensor/55/temp'
    name: 'Water'
    unit_of_measurement: '°C'


  - platform: mqtt
    state_topic: 'home/sensor/281/temp'
    name: 'Manne'
    unit_of_measurement: '°C'
  

Automation file

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

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

Wow, thanks for this one. Im trying for 2 days now and I’m on my way I guess.
Some of my switches are already appearing in HA. I’m trying to understand this automation, can you help me?

First: I figured that some of my not appearing switches need different dtype in this line:

value_template: ‘{{ trigger.payload_json.dtype == “Light/Switch” }}’

My switches are appearing as Lighting 1 or Lighting 2. So I´ve copied the complete automation 2 times more for Lighting 1 and Lighting 2. This way more DZ switches appearing in HA. Now comes the harder part for me. Those Lighting 2 switches are dimmers in fact. I guess that this dimmer is dimmed operated by payload_template “svalue1” See the the complete mqtt messages for off, on dimming and full below.

So I guess the codes needs to be changed from

  topic: 'homeassistant/switch/dz_switch_{{ trigger.payload_json.idx }}/state'
     payload_template: '{% if trigger.payload_json.**nvalue** == 1 %} On {% else %} Off {% endif %}'

To

  topic: 'homeassistant/switch/dz_switch_{{ trigger.payload_json.idx }}/state'
     payload_template: '{% if trigger.payload_**json.svalue1** == 1 %} On {% else %} Off {% endif %}'

But what to do with those percentages behind “svalue1”. Can’t figure this out yet.

{
    "Battery": 255,
    "Level": 100,
    "RSSI": 12,
    "description": "",
    "dtype": "Lighting 2",
    "hwid": "1",
    "id": "0030407",
    "idx": 793,
    "name": "koof",
    "nvalue": 2,
    "stype": "AC",
    "svalue1": "15",
    "switchType": "Dimmer",
    "unit": 1
}
{
    "Battery": 255,
    "Level": 46,
    "RSSI": 12,
    "description": "",
    "dtype": "Lighting 2",
    "hwid": "1",
    "id": "0030407",
    "idx": 793,
    "name": "koof",
    "nvalue": 2,
    "stype": "AC",
    "svalue1": "7",
    "switchType": "Dimmer",
    "unit": 1
}
{
    "Battery": 255,
    "Level": 100,
    "RSSI": 12,
    "description": "",
    "dtype": "Lighting 2",
    "hwid": "1",
    "id": "0030407",
    "idx": 793,
    "name": "koof",
    "nvalue": 1,
    "stype": "AC",
    "svalue1": "15",
    "switchType": "Dimmer",
    "unit": 1
}
{
    "Battery": 255,
    "Level": 86,
    "RSSI": 12,
    "description": "",
    "dtype": "Lighting 2",
    "hwid": "1",
    "id": "0030407",
    "idx": 793,
    "name": "koof",
    "nvalue": 0,
    "stype": "AC",
    "svalue1": "15",
    "switchType": "Dimmer",
    "unit": 1
)

Secondly: I can switch those appeared switches on in HA but after a few seconds the HA switch switches off automatically. When switching on in HA, the lamp goes on, when the switch turns off automatically the lamp stays on. What I also notice is that when quickly switching on and off the DZ switch in HA, the lamp goes on and off. For this part I like to get some pointers. Coming from Domoticz it seems that Home Assistant has a steep learning curve. :slight_smile: Thanks in advance for any feedback

Hi,

Thinks you need to search around mqtt light documentation https://www.home-assistant.io/integrations/light.mqtt/

I think the main ideas remains having a config topic which describe topics the device handle and implement such topics

1 Like

Hi all,
thanks for your code rimram31, it helped me a lot.
the parameter retain: true generating an order each time the automation is reloaded at home (for my cover s and lights) I am looking for a solution to regularly refresh the state of this domoticz devices in home assistant (I reload it regularly for development)
I would like to use the domoticz / in / getdeviceinfo command but without specifying the entire idx. have you tested it? do you think this is a good way?

1 Like

Hello, I have a device in Domoticz, type Rain.
I don’t know how to use it in Hass with mqtt.

Here is the mqtt msg from domoticz:

{
    "Battery": 255,
    "RSSI": 12,
    "description": "",
    "dtype": "Rain",
    "hwid": "15",
    "id": "82130",
    "idx": 130,
    "name": "Précipitations",
    "nvalue": 0,
    "stype": "TFA",
    "svalue1": "1",
    "svalue2": "0",
    "unit": 1
}

In Hass automation, I’ve tried this:

If anybody can help me on this ?
Thanks

Awesome thanks so much for this. I am currently making the migration from Domoticz to HomeAssistant and this is super helpful as I do not want to go the big bang approach and migrate everything in one go. One issue though It seems when I enable a light switch in HA it switches on the light but on the HA interface the switch goes to off state within a second or so but the actual light remains on. Any idea what might cause this? Same issue as what steef84 has above. Not sure if there was ever a solution for this?

@v0d0r I don’t know as I’ve checked now and I’m using the exact “switch” code above on my ha installation and it works. The best way I’ve find to investigate, I think I’ve already comment this here, is to use a mqtt client to listen mqtt messages (I’using a Linux mqtt client).

For all other questions, same answer … my original idea was to link my domoticz temperature sensors and switches I don’t want to replace (and I will not), I’ve not investigate for others components.

Listen for mqtt messages, find appropriate payloads for ha to emulate a mqtt component, don’t know but perhaps too, HA has no mqtt implementation for all components (and some are perhaps more complicated to implement)

1 Like

That is because HA receives no confirmation that your light is on.
Maybe show your light configuration and the mqtt messages, so we can help.

thanks. ill get a client and see if i can see what happening inside mqtt