Help with mqtt templating (from Domoticz)

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

Howdy,
@v0d0r did you ever get this working? I see the exact same behavior on my setup.

Cheers

I’m just switching over from Domoticz to HA, so also ran in to this issue. It is indeed because HA does not know the state.
So you have to make it such that if you set a switch in HA and send this via MQTT to Domoticz, that Domoticz reports back the state on MQTT.
By default, this “echoing” back the state is not enabled in Domoticz. So first thing you have to do is to set “prevent loop” to “false” in the Domoticz hardware settings for the MQTT Client Gateway.
Furthermore, I found it much easier if Domoticz publishes its messages on dedicated topics for each sensor, in the format domoticz/out/idx
To enable that, you have to set the “Publish Topic” in the MQTT client gateway to “Index”.
Then you can make e.g. a switch in HA that also listens to the state-info which it gets back from Domoticz.
This is how I have set that up:

switch:
  - platform: mqtt
    unique_id: 1233
    name: "Test_Switch"
    icon: mdi:lightbulb-outline
    state_topic: "domoticz/out/1233"
    value_template: "{% if value_json.nvalue == 1 -%}ON{%- else %}OFF{%- endif %}"
    command_topic: "domoticz/in"
    payload_on: '{"command":"switchlight","idx":1233,"switchcmd":"On"}'
    payload_off: '{"command":"switchlight","idx":1233,"switchcmd":"Off"}'
    state_on: "ON"
    state_off: "OFF"
    optimistic: false
    qos: 0
    retain: false

And after lot of tinkering I found out that for a dimmable light you would get this:

  - platform: mqtt
    schema: template
    unique_id: 20
    name: "Hall"
    state_topic: "domoticz/out/20"
    command_topic: "domoticz/in"
    state_template: "{% if value_json.nvalue >= 1 -%}on{%- else %}off{%- endif %}"
    brightness_template: "{{ (value_json.Level*255/100) | round(0) }}"
    command_off_template: '{"command":"switchlight","idx":20,"switchcmd":"Off"}'    
    command_on_template: >
      {"command":"switchlight","idx":20,"switchcmd":
      {%- if brightness is defined -%}
      "Set Level","level":{{ (brightness*100/255) | round(0) }}
      {%- else -%}
      "On"
      {%- endif -%}
      }

You have to replace the idx values for your own.
This is now set-up for a Zwave dimmer that accept level values from 1 - 100. If you use e.g. RFX-Com the range is from 1 - 15, so you would have to adjust the brightness to brightness*15/255

Hi Bikey,

As a complete HA newbie I tried your examples with a simple copy/paste (did change the IDX) above but no luck. I tried the second config, for dimmable lights.
It seems to be missing something in the config?
Should this be placed in the automations.yaml file or configurations.yaml? If in automation, will I need something in the configurations.yaml that refer to this?

Would be very interresting to get this going as I am in also the process of switching over from Domoticz.

Hi,

Yeah, it is a bit of a steep learning curve :wink:
The code should be placed in the configurations.yaml
Did you also add the MQTT integration in HA (via configuration/integrations)?
And alway handy to check with an mutt-client, like mosquito_sub what actually happens on the mutt-topics, both the domoticz/in (to see what HA has sent) as domoticz/out/idx (to see the response from Domoticz).

Hi,

Thank you very much for your reply.
I have configured the MQTT integration as I was able to get the temperature sensors created by using an example earlier in this thread. I use Node-Red to verify the flows coming from Domoticz. But will look into mosquito_sub, great tip.
If I take your example, can I just copy paste it as it is without any further configs/parameters? Both of your examples?
You also mention that in Domoticz I need to set the MQTT client to publish Index, in my version of Domotiz I have the following options:

  1. out
  2. /
  3. out + /
    Which one of them do you use?

Coming from Domoticz to HA is a steeper curve yes :wink:

You have to use option “index”, which als explains that everything is published on domoticz/out/idx
If you don’t have that option you may have to upgrade to a newer version…

I also use Node-red to check things, works great, as you can monitor both the domiticz/out/+ as the domoticz/in/+ at the same time.