Help with mqtt templating (from Domoticz)

I bet you can handle this with a single automation and exploit the fact that attributes of the trigger are available as variables within an automation. Creative use of scripts (they can accept arguments) or templating within an action can handle things based on the incoming ID.

Johan. I’m trying to do the same but can’t seem to get things working.

Did you manage to get the sensor readings from the different sensors? If so please show me a working config and where put things. After a two days struggling with this i’m begingen to think the move to home assistant isn’t going to work for me.

Hi there remc0!

No I spent some time on it and then lost it, but I would like to get back to it because it is something I need. I’m afraid my knowledge is to shallow in this case but I think the solution could be setting up mqtt sensor with a value template

Hi Johan,

I did it almost. I get the value but it also disappears. It’s flikkering.

- platform: mqtt
  state_topic: "domoticz/out"
  name: "domoticz_lastmsg"
  value_template: >
    {% if value_json.idx == 25 %}
      {{ value_json.svalue5 }}
    {% endif %}

It has to to with my not understanding the formatting.

What I tried to do is test if idx has value x and that works. If true get the svalue5 and put it in the sensor. that also works but if another idx passes I get notting. What I want is that the sensor ignores the other idx.

Regards

I finally solved this in an non-HA way… I take the MQTT-messages from Domoticz filter them in Node-Red and send off new mqtt-messages that are better structured for use in HA… Easy solution that works perfectly, if you have a node-red server…

So here’s one way …

I have a “common” mqtt channel for multiple iBeacons and need to extract the battery level for a specific one. I’m using automation to monitor/filter the mqtt channel and extract it’s encoded battery level. I then use the community’s variable component to store and display in the UI:

variable:
  ibeacon_battery:
    value: 0

automation:
  trigger:
    platform: mqtt
    topic: 'room_presence/#'
  condition:
    condition: template
    value_template: '{{ trigger.payload_json.id == "UUID-1-1" }}'
  action:
    service: variable.set_variable
    data_template:
      variable: ibeacon_battery
      value: '{{ trigger.payload_json.minor  | int // 2**8 }}'

Hi,

There are two solutions that I could figure out:

1.In Domoticz: place the sensor in a floorplan, set mqtt topic to “/” (basically have a separate topic for the sensor; haven’t checked if multiple floorplans could be set, so each sensor has its own). Then in HA the sensor would be:

- platform: mqtt
  state_topic: 'domoticz/out/name/floorplan'
  name: 'ws1_temperature'
  unit_of_measurement: '°C'
  value_template: '{{ value_json.svalue1 }}'
- platform: mqtt
  state_topic: 'domoticz/out/name/floorplan'
  name: 'ws1_humidity'
  unit_of_measurement: '%'
  value_template: '{{ value_json.svalue2 }}'
  1. Use flat (“out”) or flat + hierarchical ( “out + /” if needed to combine with first option above) topic for mqtt in Domoticz and set an automation that would read each sensor (no need for floorplan).
    #automation - based on the payload in the first message. If weather station has only temperature readings lose the second sensor.

    - alias: WS1
       initial_state: True
       trigger:
         - platform: mqtt
           topic: 'domoticz/out'
       condition: 
         condition: template
         value_template: '{{ "82019" in trigger.payload }}'  
       action:
         - service: mqtt.publish
           data_template:
             topic: "home/weatherstation1/temp"
             payload_template: '{{ trigger.payload_json.svalue1 }}'
         - service: mqtt.publish
           data_template:
             topic: "home/weatherstation1/hum"
             payload_template: '{{ trigger.payload_json.svalue2 }}'
    

#Sensors

- platform: mqtt  
  state_topic: 'home/weatherstation1/temp'  
  name: 'ws1_temperature'  
  unit_of_measurement: '°C'
- platform: mqtt  
  state_topic: 'home/weatherstation1/hum'  
  name: 'ws1_humidity'  
  unit_of_measurement: '%'
2 Likes

Thanks @Petrica , you saved my day :grinning:

I am using the “RTL_433 to MQTT Bridge” hassio addon, and I needed to receive a temperature sensor and a remote. The addon can only publish to a single topic, and the MQTT sensor component was confused by receiving two payloads with different structure.

I made one MQTT generic topic and two automations as you suggested that filter the messages and republish them on different topics, that can be undertood by the standard MQTT sensor and MQTT switch.

The RTL Bridge addon publishes to the “homeassistant/sensor/rtl433” topic. These are the automations:

- id: 433_repub_avidsen
  alias: 433 MQTT repub Avidsen remote
  initial_state: True
  trigger:
    - platform: mqtt
      topic: 'homeassistant/sensor/rtl433'
  condition: 
    condition: template
    value_template: '{{ "2124936672" in trigger.payload }}'  
  action:
    - service: mqtt.publish
      data_template:
        topic: "homeassistant/sensor/433_avidsen"
        payload_template: '{{ trigger.payload }}'

- id: 433_repub_temp
  alias: 433 MQTT repub Temperature sensor
  initial_state: True
  trigger:
    - platform: mqtt
      topic: 'homeassistant/sensor/rtl433'
  condition: 
    condition: template
    value_template: '{{ "Prologue sensor" in trigger.payload }}'  
  action:
    - service: mqtt.publish
      data_template:
        topic: "homeassistant/sensor/temperature"
        payload_template: '{{ trigger.payload }}'

@namadori
You’re welcome

I am looking some help in similar issue - mqtt templating.
I am integrating RF433 sonoff gateway flashed with Tasmota. The gateway sends mqtt messages to only one predefined topic
09:27:11 MQT: tele/RF433/RESULT = {"RfReceived":{"Sync":12780,"Low":400,"High":1250,"Data":"3445A2","RfKey":5}}

I am looking for a way to build conditional automation that only triggered if specific Data or RfKey received.

My automation example (which does not work is here:
- alias: Magic light toggle
hide_entity: true
trigger:
platform: mqtt
topic: tele/RF433/STATE
action:
service: light.toggle
entity_id: light.magic_led_kids
condition:
condition: template
value_template: ‘{{ “3445A2” in trigger.payload}}’

I guess my value_template is not correct, any help?

Hi,

Preformat the text before posting to help others help you (</> in the menu bar).

Use also initial state for automation, place topic and value_template inside proper delimiters such as ’ and " not ’ or “.

Is there any error in the log?

Hello, thanks

I adjusted my automation, but still no luck.

No errors in the log

- alias: 'Magic light toggle'
  initial_state: true
  hide_entity: true
  trigger:
    platform: mqtt
    topic: tele/RF433/RESULT
  action:
    service: light.toggle
    entity_id: light.magic_led_kids
  condition:
    condition: template
    value_template: '{{ "3445A2" in trigger.payload }}'

Also:

topic: 'tele/RF433/RESULT'

Do you know how to check if the MQTT payload is received in the correct topic?

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