Help with mqtt templating (from Domoticz)

@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.

Ok, then I will try to figure that out on the Domoticz side. I am running the latest stable version. (Updated: Just upgraded to latest Beta, got Index now :wink: )
Got your switch example to work, but I am struggle to get the dimmable light example to work. When I validate it in HA it complains about this:
Invalid config for [switch.mqtt]: [schema] is an invalid option for [switch.mqtt]. Check: switch.mqtt->schema. (See ?, line ?).

I created a dedicated switches.yaml file where I place all the switches going forward. It looks like this:

  - platform: mqtt
    unique_id: 14
    name: "Light-Hall2"
    icon: mdi:lightbulb-outline
    state_topic: "domoticz/out/14"
    value_template: "{% if value_json.nvalue == 1 -%}ON{%- else %}OFF{%- endif %}"
    command_topic: "domoticz/in"
    payload_on: '{"command":"switchlight","idx":14,"switchcmd":"On"}'
    payload_off: '{"command":"switchlight","idx":14,"switchcmd":"Off"}'
    state_on: "ON"
    state_off: "OFF"
    optimistic: false
    qos: 0
    retain: false
  - platform: mqtt
    schema: template
    unique_id: 364
    name: "Light-Kitchen1"
    state_topic: "domoticz/out/364"
    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":364,"switchcmd":"Off"}'    
    command_on_template: >
      {"command":"switchlight","idx":364,"switchcmd":
      {%- if brightness is defined -%}
      "Set Level","level":{{ (brightness*100/255) | round(0) }}
      {%- else -%}
      "On"
      {%- endif -%}
      }

Update, and a good one also :wink:
I reckoned it must have been something obvious, as I am on a very basic level with HA. If I read the error a couple of times I should have gotten it. “Switch” does not use “template”. So I went ahead and created an another element in my config.yaml point to lights: !include lights.yaml where I pasted in the dimmable light config.

switches.yaml:

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

Lights.yaml

- platform: mqtt
    schema: template
    unique_id: 364
    name: "Light-Kitchen1"
    state_topic: "domoticz/out/364"
    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":364,"switchcmd":"Off"}'    
    command_on_template: >
      {"command":"switchlight","idx":364,"switchcmd":
      {%- if brightness is defined -%}
      "Set Level","level":{{ (brightness*100/255) | round(0) }}
      {%- else -%}
      "On"
      {%- endif -%}
      }

Thanks alot for your help Bikey, now I have the basics and can progress a lot more on my own. Learnt something also during this :wink:

My project adding all my Domoticz devices in HA with MQTT is progressing.
There are ofcourse a lot of details and room for improvement in my configs, such as the dimmer slider.
The lights work, aslo adjusting brightness, but the slider does not retain its position after the brightness has been sat to a position.
As if the brightness state is not being reported back to the switch. Does anyone know why that is? The on/off button is retained but not the slider.
Update:
It seems to be something with how Domoticz receives or publishes the MQTT message. Because there are some lights that just works, but others dont. I cant see any difference the MQTT messages either… Need to investigate further.

Well, it turned out to be so simple that I actually had to add the device itself in the Domoticz gui. It was not enough that it was only in the Devices List, it had to be added also…

1 Like

Mmm, that is a hard thing to find out. Thanks for the tip and update :wink:

While we are learning: I have found that - in contrast with Domoticz - it looks like automations are only triggered when there is a state change, e.g. from “on” to “off” or vice versa.
However, I also would to like to trigger automations based on a device that can sent “on” -> “on” events.

This is the use case:
I have a script that needs to catch the events from a (KaKu) remote that I use to dim my lights in steps: with each press of the “on” button on the remote the light need to be turned up by 20% until 100% and then cycles back to 20%. (the “off” button, just turns the lights off, and if the lights are “off”, the first press of the “on” button just turns them on).

In this way you can use cheap/simpe remotes that do not support dimming, but only “on” and “off” to control a dimmable light.

I have the automation working in HA by manually triggering, but I can not get it to work to have it triggered by “on” -> “on” events.

Does anybody know how generate a trigger from these “stateless” triggers (just looking at the “changed” attribute)?

Hi,

I wish to refresh this subject.
As i am a beginner in MQTT i wonder myself how to send the messages from domoticz.
I understood the label is “domoticz/out”
In the config of the plugin.But how to direct the values of sensors through this.Do you need a script or what?
Thanks you

Knasson

@knasson briefly to explain you, domoticz is able to send MQTT messages when devices events happen (domoticz/out) and in the other way can listen to MQTT messages to make actions (domoticz/in).

In the same way HA can send and listen MQTT messages to control devices, including automatic discovery. But the problem is that domoticz use its own MQTT logic/message structure and HA too (even if HA use a more standard way)

So the idea in my preceding post Help with mqtt templating (from Domoticz) - #35 by rimram31 is to use HA automation to listen to MQTT messages to translate dz message to HA and HA messages to dz. The compute part is done by HA automation.

Now that also Domoticz supports HA style mqtt autodiscovery I was wondering would it be possible to use this to create a more standard (device type agnostic) integration between the two. For me, Domoticz the primary system and I use HA for the devices which Domoticz does not support yet. So I’ve been looking for some nice way to achieve a two-way integration. So far I’ve been using the REST APIs of foth systems and while it works, there’s a lot of manual work involved.

@rimram31 - you mention in Help with mqtt templating (from Domoticz) - #35 by rimram31

I’ve in the middle time work on some automatic discovery version.

Where you able to progress with this?

I’m using Domoticz as primary system as well and I managed to use MQTT for integration in both direction with great success. I prefered no to use autodiscovery because I prefer to take control on every details. As soon as I have defined a way of handling different device types it is pretty easy to manage.
Here is some code snippet (publish to DZ)

  - id: 2a464810-8106-476b-a1ba-06dadba8994b
    alias: Publish to DZ (switch, sensor, input_boolean, counter, proximity)
    description: ''
    variables:
      entities:
        switch.pressostat: 644
        sensor.pressostat_power: 647
        sensor.pressostat_voltage: 653
        binary_sensor.pressostat_overpowering: 652
        switch.alimentation_electrovannes: 645
        sensor.alimentation_electrovannes_power: 648
        switch.fil_chauffant: 646
        sensor.fil_chauffant_power: 649
        switch.shellypro4pm_083af27c0770_switch_3: 631
        sensor.shellypro4pm_083af27c0770_switch_3_power: 632
        input_boolean.arrosage_master: 659
        counter.eau: 666
        proximity.nicolas_home: 670
      index: '{{ entities.get(trigger.entity_id, 0) }}'
      alert:
        'off': 1
        'on': 4
      alert_level: '{{ alert.get(trigger.to_state.state, 0) }}'
      alert_value:
        'off': Dépassement de puissance résolu
        'on': Dépassement de puissance
      alert_text: '{{ alert_value.get(trigger.to_state.state, 0) }}'
    trigger:
    - platform: state
      entity_id:
      - switch.pressostat
      - sensor.pressostat_power
      - sensor.pressostat_voltage
      - binary_sensor.pressostat_overpowering
      - switch.alimentation_electrovannes
      - sensor.alimentation_electrovannes_power
      - switch.fil_chauffant
      - sensor.fil_chauffant_power
      - switch.shellypro4pm_083af27c0770_switch_3
      - sensor.shellypro4pm_083af27c0770_switch_3_power
      - input_boolean.arrosage_master
      - input_boolean.arrosage_ev_atelier
      - input_boolean.arrosage_ev_maison_journalier
      - input_boolean.arrosage_ev_jardin_devant
      - counter.eau
      - proximity.nicolas_home
    condition: '{{ index != 0 }}'
    action:
    - service: mqtt.publish
      data:
        topic: domoticz/in
        payload_template: >
          {% if trigger.to_state.domain == 'switch' or trigger.to_state.domain == 'input_boolean'%}
            {"command": "switchlight", "idx": {{ index }}, "switchcmd": "{{ trigger.to_state.state | title }}"}
          {% elif trigger.to_state.domain == 'sensor' %}
            {"command": "udevice", "idx": {{ index }}, "nvalue": 0,  "svalue": "{{ trigger.to_state.state }}"}
          {% elif trigger.to_state.domain == 'proximity' %}
            {"command": "udevice", "idx": {{ index }}, "nvalue": 0,  "svalue": "{{ trigger.to_state.state | multiply(100) }} "}
          {% elif trigger.to_state.domain == 'binary_sensor' %}
            {"command": "udevice", "idx": {{ index }}, "nvalue": {{ alert_level }}, "svalue": "{{ alert_text }}"}
          {% elif trigger.to_state.domain == 'counter' %}
            {"command": "udevice", "idx": {{ index }}, "nvalue": 0, "svalue": "{{trigger.to_state.state | int + state_attr('counter.eau', 'initial')  }}"}                                                   
          {% endif %}
        qos: '0'
        retain: true
    mode: queued
    max: 25

likewise to receive from DZ

  - id: 93404438-01a9-4f91-933f-1d3cfc823b20
    alias: Receive from DZ (switch or input_boolean)
    description: ''
    variables:
      indexes:
        644: switch.pressostat
        645: switch.alimentation_electrovannes
        646: switch.fil_chauffant
        631: switch.shellypro4pm_083af27c0770_switch_3
        659: input_boolean.arrosage_master
        660: input_boolean.arrosage_ev_atelier
        661: input_boolean.arrosage_ev_maison_journalier
        662: input_boolean.arrosage_ev_jardin_devant
      entity: '{{ indexes.get(trigger.topic.split(''/'') | last | int(0), ''unknown'')
        }}'
      function: '{{ entity.split(".")[0] }}'
    trigger:
    - platform: mqtt
      topic: domoticz/out/#
    condition: '{{ entity != ''unknown'' }}'
    action:
    - service: '{{function}}.turn_{{ iif(trigger.payload_json.nvalue == 0, "off", "on") }}'
      target:
        entity_id: '{{ entity }}'
    mode: queued
    max: 25
  - id: aeea0bd8-e977-4aa4-a77d-1401b2d259f3
    alias: XMC-1 Volumes DZ to HA
    description: ''
    variables:
      indexes:
        619: input_number.idx619
        612: input_number.idx612
        613: input_number.idx613
        614: input_number.idx614
        615: input_number.idx615
      entity: '{{ indexes.get(trigger.topic.split(''/'') | last | int(0), ''unknown'')
        }}'
    trigger:
    - platform: mqtt
      topic: domoticz/out/#
    condition: '{{ entity != ''unknown'' }}'
    action:
      service: input_number.set_value
      data:
        entity_id: '{{ entity }}'
        value: '{% set v_pct = trigger.payload_json.svalue1 | int / 100 %} {{ iif (
          entity == ''input_number.idx619'', ((v_pct * 75) - 70), ((v_pct * 32) - 16)
          ) | round(0) }}'
    mode: queued
    max: 25

1 Like

Thanks, this is great. One thing where I got a bit stuck was these climate entites in Home Assistant that contain several values (temperature, current_temperature, hvac_mode etc). How could I monitor for changes in certain attribute and the publish based on that? Only thing I was able to come up with was to have separete checks for each from_state vs. to_state. This is my current actions:

  action:
  - service: mqtt.publish
    data:
      topic: domoticz/in
      payload_template: >
        {% if trigger.to_state.attributes.device_class == 'door' and trigger.to_state.state in ['on','off'] %}
          {"command": "switchlight", "idx": {{ index }}, "switchcmd": "{{ trigger.to_state.state | title }}"}
        {% elif trigger.to_state.attributes.unit_of_measurement == 'min' %}
          {"command": "udevice", "idx": {{ index }}, "nvalue": 0, "svalue": "{{ trigger.to_state.state }}"}
        {% elif trigger.to_state.attributes.friendly_name == 'Thermostat 1' and trigger.to_state.attributes.temperature !=  trigger.from_state.attributes.temperature %}
          {"command": "udevice", "idx": {{ index }}, "nvalue": 0, "svalue": "{{ state_attr('climate.thermostat_1', 'temperature') }}"}
        {% elif trigger.to_state.attributes.friendly_name == 'Thermostat 1' and trigger.to_state.attributes.current_temperature !=  trigger.from_state.attributes.current_temperature %}
          {"command": "udevice", "idx": {{ (index|int) + 3 }}, "nvalue": 0, "svalue": "{{ state_attr('climate.thermostat_1', 'current_temperature') }}"}
        {% endif %}

Any comments on how to improve (it works in current format as well). Btw, what is the purpose of the “title” in “trigger.to_state.state | title”?

| title is a filter function that capitalize the first letter of a sentence. It is needed because HA states are in lowercase (on/off) and Domoticz expects On or Off as per documentation Domoticz MQTT
As for the rest of your question, I don’t see how to improve your current code, except that if you have several climate entities I would replace:

"{{ state_attr('climate.thermostat_1', 'temperature') }}"

by

"{{ state_attr('climate.thermostat_' ~id , 'temperature') }}"

id would be extracted from the entity that triggered the automation. As an example see this snippet

- id: 69472b24-9c31-41f7-b324-b285fec1d7cf
    alias: Chauffage PID
    description: Pilotage des radiateurs par les thermostats PID
    trigger:
    - platform: state
      id: command
      entity_id:
      - input_boolean.heater_command_id3
      - input_boolean.heater_command_id4
      - input_boolean.heater_command_id5
      - input_boolean.heater_command_id6
    - platform: state
      id: climate
      entity_id:
        - climate.device_pid_id3 # salle de bains
        - climate.device_pid_id4 # salle de bains amis
        - climate.device_pid_id5 # chambre
        - climate.device_pid_id6 # chambre amis
    - platform: template
      id: new_set_value
      value_template: >
        {{state_attr("climate.device_pid_id3", "temperature")}}
    - platform: template
      id: new_set_value
      value_template: >
        {{state_attr("climate.device_pid_id4", "temperature")}}
    - platform: template
      id: new_set_value
      value_template: >
        {{state_attr("climate.device_pid_id5", "temperature")}}
    - platform: template
      id: new_set_value
      value_template: >
        {{state_attr("climate.device_pid_id6", "temperature")}}
    variables:
      index: 
        "id3": "143:select" # commande radiateur salle de bains
        "id4": "531:select" # commande radiateur salle de bains amis
        "id5": "228:select" # commande radiateurs chambre
        "id6": "229:select" # commande radiateurs chambre amis
      id: "{{trigger.entity_id.split('_') | last }}" # entity index (id1, id2, ...)
      idx: "{{index.get(id).split(':')[0]}}" # entity to control (dz idx) or climate entity
    condition: []
    action:
    - if:
      - condition: trigger
        id: climate
      - condition: template
        value_template: >
          {{states("climate.device_pid_" ~ id) == "off"}}
      then:
      - service: select.select_option
        data:
          option: "Off"
        target:
          entity_id: "select.{{idx}}"
      else:
        - condition: trigger
          id: command
        - if:
          - condition: template
            value_template: >
              {{states("climate.device_pid_" ~ id) == "heat"}}
          then:
            # Command depends on difference between consigne et current in order to avoid switching off heaters constantly.
            # Fil pilote 6 ordres: using Eco, -1° and -2°
            # Fil pilote 4 ordres: using Eco only
            # Off or HG is used only when target and consigne are very close
            # TBD        
            - service: select.select_option
              data:
                option: >
                  {% if state_attr("climate.device_pid_" ~ id , "temperature") | float() > 8 %}
                    {% set cmd = "HG" %}
                  {% else %}
                    {% set cmd = "Off" %}
                  {% endif %} 

                  {% if id == 5 or id == 6 %}
                    {{ iif(trigger.to_state.state == "on", "On", cmd) }}
                  {% else %}
                    {{ iif(trigger.to_state.state == "on", "On", cmd) }}
                  {% endif %}
              target:
                entity_id: select.{{idx}}
        # - condition: trigger
        #   id: new_set_value
        # - service: smart_thermostat.clear_integral
        #   data: {}
        #   target:
        #     entity_id: climate.device_pid_{{id}}

    mode: parallel
    max: 20

Thanks - a nice addition as I indeed have several thermostats. However, the current naming convention does not support to derive the name dynamically, but will definitely think about if I should make some changes there…

Hi everybody,

I can’t figure out why my custom mqtt sensor (temp from domoticz) doesn’t show up :
configuration.yaml

# Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

# Text to speech
tts:
  - platform: google_translate

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

http:
  use_x_forwarded_for: true
  trusted_proxies: 192.168.1.4

# Sensors
mqtt:
  sensor:
    - state_topic: "homeassistant/sensor/230/temp"
      name: "Etage Temp"
      unit_of_measurement: "°C"
      device_class: "temperature"

automations.yaml

- id: '1582583923'
  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: 'homeassistant/sensor/{{ trigger.payload_json.idx }}/temp'
         payload_template: '{{ trigger.payload_json.svalue1 }}'
                
- id: '1582583924'
  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: 'homeassistant/sensor/{{ trigger.payload_json.idx }}/temp'
         payload_template: '{{ trigger.payload_json.svalue1 }}'
     - service: mqtt.publish
       data_template:
         topic: 'homeassistant/sensor/{{ trigger.payload_json.idx }}/hum'
         payload_template: '{{ trigger.payload_json.svalue2 }}'

And mqtt explorer :

Has somebody got an idea where my mistake is ?

Thx