Help with mqtt templating (from Domoticz)

Yep, I am subscribed to the topic in my linux pc terminal and I can see the json messages being published by RF hub
Client mosqsub|4809-aleksejs-p received PUBLISH (d0, q0, r0, m0, ‘tele/RF433/RESULT’, … (76 bytes))
{“RfReceived”:{“Sync”:10240,“Low”:320,“High”:940,“Data”:“CA2F0E”,“RfKey”:4}}

I don’t see 3445A2 in the payload so it is normal that the automation doesn’t fire. Is it possible that the device sends two codes? What type is it (PIR/OpenDoor sensor)?

Yes, you are right in the sense the code in not in the payload. Just picked random example from the output in the terminal.
Here is the relevant mqtt message.

Client mosqsub|5003-aleksejs-p received PUBLISH (d0, q0, r0, m0, 'tele/RF433/RESULT', ... (77 bytes))
{"RfReceived":{"Sync":12770,"Low":410,"High":1230,"Data":"3445A2","RfKey":5}}

The device only sends one code (pushbutton), therefore used light.toggle service.

Can you toggle light from other triggers?

I mean are you sure the “light.magic_led_kids” corresponds to an existing entity and there isn’t any typo? Can you check in dev-state if it is really this entity name?

When you are sending the code keep the terminal monitoring “tele/RF433/RESULT” topic and see if it sends the same code (it might be that a different code is received).

Many thanks for your time.
The last automation is working and can be reused.
There were other rogue automations (resetting the binary sensors) that upset comm between MQTT broker and RF hub. I had to temporary disable those until another workable solution is found .

You’re welcome.

As you are using a single MQTT topic for several binary sensors you might want to have a look at the following discussion as the current HA config is going to flood your log with warnings about the payload (the updated file is at post 17):

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