Help with mqtt templating (from Domoticz)

I’m wondering if someone can help me out. I’m running a Domoticz instance to gather data from NIBE heatpump API (https://www.domoticz.com/forum/viewtopic.php?t=13640) and would of course like to move some of this data into HA… I’ve setup Domoticz to send MQTT messages as value updates and they look like this:

domoticz/out

{
“Battery” : 255,
“RSSI” : 12,
“dtype” : “Temp”,
“id” : “82019”,
“idx” : 19,
“name” : “F1255 Return Temp.”,
“nvalue” : 0,
“stype” : “LaCrosse TX3”,
“svalue1” : “45.3”,
“unit” : 1
}

What I’m failing to do is to write some value-template to extract this. Since all messages are received in the same topic I’m thinking that I should like filter a sensor for this on the id 82019 and store the svalue 45.3.

I’ve gotten this far:

  • platform: mqtt
    name: “Kitchen temp”
    state_topic: “domoticz/out”
    unit_of_measurement: “C”
    value_template: ‘{{ value_json.svalue1 }}’

But any solution for filtering on the id fails miserably… Anyone that could give me some pointers?

/Johan

Editing subject and bumping this thread to see if there is any template gurus out there who can give me a clue…

Are you saying you’re having trouble extracting the ID from that JSON?

Isn’t it simply {{value_json.id}}

Thank for looking into this! I guess that should work but I’m trying to extract svalue1 WHEN id=X

Domoticz sends all sensors on domoticz/out so svalue1 changes several times every five minutes since there are several sensors having this value, all with different ID’s. So my idea, which I failed to put into action, was to setup several sensor and write a template that extracted the right value for each, identified by id.

/Johan

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.