Write Update Values through MQTT?

Hi,

I have a device which sends status notification through a MQTT channel. I am able to read all values and display just works fine.

The device offers updating some of these values through a different MQTT topic. So I can control this device through MQTT-json like this:

{
   "name": "hzgcmd",
   "unixtime": 1727382965,
   "status": {
     "heizung": true,
     "manuallen": 30,
     "readcfg": false
   },
   "relais": {
     "r_kluefter": false
   }
 }

So in this case it sets a vent to “false”.

Do you guys have any ideas how I can implement a device which can send control updates through MQTT?

Any hints or links, please?

Thanks!

/KNEBB

Using mqtt explorer check the topics around the device. If you see something like commands or cmnd you may be able to publish to that topic and change things.

Hi,

well, yes. I can use any MQTT client to publish items there.

My question tends to ask how I can implement it in HomeAssistant?

/KNEBB

action:
  - data:
      topic: outside/sleepearly
      qos: "1"
      payload: "ON"
    action: mqtt.publish

Easily done through automations or scripts.

Payloads can be more complex

  - data:
      topic: tinygs-servo/sleep_mode_time
      payload_template: >-
        {{ (((((states('proximity.home')|float-2100)**2
        )**0.5)/6.5)+15)|round(0)*1000 }}
      qos: "1"
      retain: false
1 Like

you can use actions (new name for service calls) or you can use NodeRed if you find it more comfortable (I do)

1 Like

Hmmm… let me try to understand.

So I have to create an automation with an action where I have to define the mqtt.publish action within, right?

But what am I supposed to define as trigger? Generate a dummy device which I can set to on or off?

So this would be my first attempt for the automation header:

automation:
  # Publish to the mqtt topic a JSON message
  trigger:
    - # What do I have to use as trigger? a dummy device which gets set to a new value (ie 100)?

  # action will be used to perform the publishing
  action:
    - action: mqtt.publish
      topic: /device/cmd/ # the mqtt topic where I want to publish
payload: >-
    { "name": "haoscmd", 
    "unixtime": <INSERT current Unix time here>,
     "relais": { "r_wp": <INSERT 1 or 0> }   
    }

Is this correct so far? Do I have to create seperate actions for 0 or 1? How do I insert the current unix time?

Thanks so far!

/KNEBB

Automation is one way of publishing to mqtt.

alias: publish backroom temp sensor to mqtt
description: ""
trigger:
  - platform: time_pattern
    minutes: /1
condition: []
action:
  - action: mqtt.publish
    metadata: {}
    data:
      qos: 0
      retain: true
      payload: "{{states('sensor.backroom_temp') | float (0) }}"
      topic: sensor/backroom_temp
mode: single

This is a trigger which runs every minute. You can use choose to do different actions based on conditions within one automation or 2 different automations. You can trigger the publish anyway you want as it’s very flexible. Yes you can use an Input Boolean “a dummy device” to trigger.
Your YAML isn’t formatted correctly. Use the visual editor in automations to guide you. I don’t know how to insert the unix time but I suspect you would use sensor.time entity.

Hi,

really helpful- thanks a lot!

This is what I got so far:

alias: MQTT Publish
description: ""
trigger:
  - platform: time_pattern
    minutes: /5
condition: []
action:
  - action: mqtt.publish
    data:
      qos: "0"
      retain: true
      topic: hzg/command
      payload: "{ name: hzgcmd, time:{{ as_timestamp(now()) }} }"
mode: single

Still missing is the parameter to insert after the timestamp. But I guess I can use the state() command from my dummy device.
I.e. user switches on the dashboard the dummy device to “ON” the mqtt will be using the new value then.

I guess I am done here so far as the rest I am going to figure out on my own.

I appreciate your help!

/KNEBB

1 Like