How to get the zwave device status from MQTT in a python script

Topics (not sure which fora)
when not sure where to post, post here and mods will move it to right forum.

Moderators: leecollings, remb0

Post Reply
meal
Posts: 114
Joined: Monday 04 March 2019 14:59
Target OS: Raspberry Pi / ODroid
Domoticz version: 2025.2
Location: France
Contact:

How to get the zwave device status from MQTT in a python script

Post by meal »

Hello,

Context:
Version: 2023.2
Build Hash: 19efd039c
Compile Date: 2023-07-21 17:23:44
dzVents Version: 3.1.8
Python Version: 3.9.2 (default, Mar 12 2021, 04:06:34) [GCC 10.2.1 20210110]

ZwaveJS UI installed
Prefix : zwave
Name : zwave-js-ui

paho-mqtt package installed

Issue :
How to get the zwave device status from MQTT in a python script
from the url https://zwave-js.github.io/zwave-js-ui/ ... ode-status
there is a MQTT topic : <mqtt_prefix>/<?node_location>/<node_name>/status

Beforehand I wrote a simple python script to get the MQTT info using api getInfo :

Code: Select all

import paho.mqtt.client as mqtt #import the client
import time

def on_message(client, userdata, message):
    print("message received " ,str(message.payload.decode("utf-8")))
    print("message topic=",message.topic)
    print("message qos=",message.qos)
    print("message retain flag=",message.retain)

def on_publish(client,userdata,result): #create function for callback
    print("begin data published \n")
    print(result)
    print("end data published \n")

def on_disconnect(client, userdata, rc):
    print("client disconnected ok")

def on_log(client, userdata, level, buf):
    print("log: ",buf)


broker_address="192.168.0.112"
client = mqtt.Client("zwave") #create new instance
client.on_publish = on_publish #assign function to callback
client.on_disconnect = on_disconnect
client.on_log=on_log
client.on_message=on_message #attach function to callback
client.connect(broker_address,1883) #connect to broker
publish_result=client.publish("zwave/_CLIENTS/ZWAVE_GATEWAY-zwave-js-ui/api/getInfo/set","payload")
print(publish_result.rc)
print(publish_result.is_published())
publish_result.wait_for_publish()
print(publish_result.is_published())

I do not get the Gatevay info and get stuck in the line publish_result.wait_for_publish()

Has anyone developed a python script to access to zwave-js-ui via MQTT or could help me ?

BR
meal
User avatar
waltervl
Posts: 6689
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2025.1
Location: NL
Contact:

Re: How to get the zwave device status from MQTT in a python script

Post by waltervl »

Reading a mqtt topic is subscribing. Publishing is writing. So instead of reading you try to write to the topic.

Google for "paho mqtt subscribe"

Edit: I found this minimal example:
change "my-topic" to for example "zwave/_CLIENTS/ZWAVE_GATEWAY-zwave-js-ui/api/getInfo/set"

Code: Select all

#!/usr/bin/env python3
import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):
    # This will be called once the client connects
    print(f"Connected with result code {rc}")
    # Subscribe here!
    client.subscribe("my-topic")

def on_message(client, userdata, msg):
    print(f"Message received [{msg.topic}]: {msg.payload}")

client = mqtt.Client("mqtt-test") # client ID "mqtt-test"
client.on_connect = on_connect
client.on_message = on_message
client.connect('127.0.0.1', 1883)
client.loop_forever()  # Start networking daemon
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
meal
Posts: 114
Joined: Monday 04 March 2019 14:59
Target OS: Raspberry Pi / ODroid
Domoticz version: 2025.2
Location: France
Contact:

Re: How to get the zwave device status from MQTT in a python script

Post by meal »

Hello

I managed to get the status of a node

Code: Select all

import paho.mqtt.client as mqtt #import the client1
import time

def on_message(client, userdata, message):
    print("message received " ,str(message.payload.decode("utf-8")))
    print("message topic=",message.topic)
    print("message qos=",message.qos)
    print("message retain flag=",message.retain)

def on_publish(client,userdata,result):             #create function for callback
    print("begin data published \n")
    print(result)
    print("end data published \n")

def on_disconnect(client, userdata, rc):
    print("client disconnected ok")

def on_log(client, userdata, level, buf):
    print("log: ",buf)


broker_address="192.168.0.112" 
client = mqtt.Client("zwave") #create new instance
client.on_publish = on_publish                          #assign function to callback
client.on_disconnect = on_disconnect
client.on_log=on_log
client.on_message=on_message #attach function to callback
client.connect(broker_address,1883) #connect to broker
client.loop_start() #start the loop
subscribe_result=client.subscribe("zwave/nodeID_2/status",qos=1)

I get the answer in the onMessage call back:


message received {"time":1694391674230,"value":true,"status":"Alive","nodeId":2}
message topic= zwave/nodeID_2/status
message qos= 1
message retain flag= 1


@waltervl : Do you know how I could get the Battery Level information ?

BR
meal
meal
Posts: 114
Joined: Monday 04 March 2019 14:59
Target OS: Raspberry Pi / ODroid
Domoticz version: 2025.2
Location: France
Contact:

Re: How to get the zwave device status from MQTT in a python script

Post by meal »

Hello,

I managed to get the battery level information of a Fibaro Door Sensor (FGDW002) nodeID_3 from MQTT in a python script :

subscribe_result=client.subscribe("zwave/nodeID_3/128/0/level",qos=1)
log: Sending SUBSCRIBE (d0, m4) [(b'zwave/nodeID_3/128/0/level', 1)]
>>> log: Received SUBACK
log: Received PUBLISH (d0, q1, r1, m4), 'zwave/nodeID_3/128/0/level', ... (66 bytes)
message received {"time":1696182955054,"value":100,"nodeName":"","nodeLocation":""}
message topic= zwave/nodeID_3/128/0/level
message qos= 1
message retain flag= 1
log: Sending PUBACK (Mid: 4)

so Battery level is 100% in my case.

However in Domoticz the battery level of the devices attached to the nodeID_3 namely nodeID_3_door_state_simple, nodeID_3_notification_access_control_door_state, nodeID_3_air_temperature,nodeID_3_battery_islow, nodeID_3_battery_maintenance_status, nodeID_3_cover_status is 255.

It seems that for that Fibaro FGDW002 device the battery level is not updated correctly in Domoticz from MQTT.

Is it a bug or do I miss something in the configuration.

BR
meal
User avatar
waltervl
Posts: 6689
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2025.1
Location: NL
Contact:

Re: How to get the zwave device status from MQTT in a python script

Post by waltervl »

Battery level should work from MQTT auto discover out of the box. There should be no need to update this externally.

There is an existing Github issue for battery level for a specific device. Perhaps your device has the same issue.
https://github.com/domoticz/domoticz/issues/5806
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
meal
Posts: 114
Joined: Monday 04 March 2019 14:59
Target OS: Raspberry Pi / ODroid
Domoticz version: 2025.2
Location: France
Contact:

Re: How to get the zwave device status from MQTT in a python script

Post by meal »

Hello,

It is not my intention to update the battery level externally.

The thing is there is a discrepancy between the battery level in MQTT (100%) and the one shown in Domoticz which value is 255.

I don't understand how to explain such discrepancy.

BR
meal
User avatar
waltervl
Posts: 6689
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2025.1
Location: NL
Contact:

Re: How to get the zwave device status from MQTT in a python script

Post by waltervl »

Possibly because Zwave-JS-UI is sending 255 to Domoticz. You should be able to see that in MQTT explorer.
And I do not think ZwaveJS allows you to modify the battery level on ZwaveJS as it is a value coming from the device.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
meal
Posts: 114
Joined: Monday 04 March 2019 14:59
Target OS: Raspberry Pi / ODroid
Domoticz version: 2025.2
Location: France
Contact:

Re: How to get the zwave device status from MQTT in a python script

Post by meal »

Hello,
I confirm the issue I encounter looks like the one you mentioned before : https://github.com/domoticz/domoticz/issues/5806
BR
meal
User avatar
waltervl
Posts: 6689
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2025.1
Location: NL
Contact:

Re: How to get the zwave device status from MQTT in a python script

Post by waltervl »

Then perhaps mention that in the GitHub issue and be prepared to deliver additional mqtt auto discover information about your devices to help to solve the issue.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
meal
Posts: 114
Joined: Monday 04 March 2019 14:59
Target OS: Raspberry Pi / ODroid
Domoticz version: 2025.2
Location: France
Contact:

Re: How to get the zwave device status from MQTT in a python script

Post by meal »

Hello,

After a while the battery level of the 4 devices attached to the nodeID_3 namely "nodeID_3_door_state_simple","nodeID_3_notification_access_control_door_state"," nodeID_3_air_temperature","nodeID_3_cover_status" is 100 and in sync with MQTT.

I did a couple of tests:
- open/close the door
- force a temperature change
- remove cover
- wake up the sensor
- replace battery
- start-stop domoticz
and during all these tests the battery level is kept in sync in Domoticz and MQTT and ZwaveJSUI.

It seems that when including the sensor the battery level is not known and value in Domoticz is 255.
It takes some time before the battery level is updated in Domoticz when receiving a new change of the sensor.

BR
meal
meal
Posts: 114
Joined: Monday 04 March 2019 14:59
Target OS: Raspberry Pi / ODroid
Domoticz version: 2025.2
Location: France
Contact:

Re: How to get the zwave device status from MQTT in a python script

Post by meal »

Hello,

The battery level is not correctly updated after a Fibaro FGDW002 sensor wake up.
The sequence of the operations is as follow:

When receiving the WakeUpNotification the system :
- queries the Air Temperature value from the Fibaro FGDW002 sensor
- updates the Air Temperature Domoticz device accordingly and at that time the battery level of the Air Temperature Domoticz device is set to 255.
- sends the node back to sleep

See the details collected from Zwave JS UI logs:

2023-10-11T05:09:17.352Z DRIVER « [Node 003] [REQ] [ApplicationCommand]
└─[WakeUpCCWakeUpNotification]
2023-10-11T05:09:17.354Z CNTRLR « [Node 003] received wakeup notification
2023-10-11T05:09:17.379Z CNTRLR [Node 003] The node is now awake.
2023-10-11T05:09:17.392Z CNTRLR » [Node 003] Multilevel Sensor CC values may be stale, refreshing...
2023-10-11T05:09:17.393Z CNTRLR » [Node 003] querying Air temperature sensor reading...
2023-10-11T05:09:17.404Z CNTRLR [Node 003] No scale preference for sensor type 1, using the last-used scale 0
2023-10-11T05:09:17.416Z DRIVER all queues busy
2023-10-11T05:09:17.423Z SERIAL » 0x010b0013030431040100256091 (13 bytes)
2023-10-11T05:09:17.424Z DRIVER » [Node 003] [REQ] [SendData]
│ transmit options: 0x25
│ callback id: 96
└─[MultilevelSensorCCGet]
sensor type: Air temperature
scale: Celsius
2023-10-11T05:09:17.433Z SERIAL « [ACK] (0x06)
2023-10-11T05:09:17.435Z SERIAL « 0x0104011301e8 (6 bytes)
2023-10-11T05:09:17.435Z SERIAL » [ACK] (0x06)
2023-10-11T05:09:17.436Z DRIVER « [RES] [SendData]
was sent: true
2023-10-11T05:09:17.558Z SERIAL « 0x010700136000000d86 (9 bytes)
2023-10-11T05:09:17.559Z SERIAL » [ACK] (0x06)
2023-10-11T05:09:17.560Z DRIVER « [REQ] [SendData]
callback id: 96
transmit status: OK
2023-10-11T05:09:17.774Z SERIAL « 0x010c00040003063105012200df3a (14 bytes)
2023-10-11T05:09:17.775Z SERIAL » [ACK] (0x06)
2023-10-11T05:09:17.777Z CNTRLR [Node 003] [Multilevel Sensor] Air temperature: metadata updated [Endpoint 0]
2023-10-11T05:09:17.780Z CNTRLR [Node 003] [~] [Multilevel Sensor] Air temperature: 22.3 => 22.3 [Endpoint 0]
2023-10-11T05:09:17.786Z DRIVER « [Node 003] [REQ] [ApplicationCommand]
└─[MultilevelSensorCCReport]
type: Air temperature
scale: Celsius
value: 22.3
2023-10-11T05:09:17.787Z CNTRLR « [Node 003] received current Air temperature sensor reading: 22.3 °C
2023-10-11T05:09:17.792Z DRIVER all queues idle
2023-10-11T05:09:18.796Z CNTRLR » [Node 003] Sending node back to sleep...
2023-10-11T05:09:18.822Z DRIVER all queues busy
2023-10-11T05:09:18.826Z SERIAL » 0x010900130302840824004c (11 bytes)
2023-10-11T05:09:18.827Z DRIVER » [Node 003] [REQ] [SendData]
│ transmit options: 0x24
│ callback id: 0
└─[WakeUpCCNoMoreInformation]
2023-10-11T05:09:18.830Z SERIAL « [ACK] (0x06)
2023-10-11T05:09:18.836Z SERIAL « 0x0104011301e8 (6 bytes)
2023-10-11T05:09:18.837Z SERIAL » [ACK] (0x06)
2023-10-11T05:09:18.838Z DRIVER « [RES] [SendData]
was sent: true
2023-10-11T05:09:18.849Z CNTRLR [Node 003] The node is now asleep.
2023-10-11T05:09:18.854Z DRIVER all queues idle

Is it a bug ?

BR
meal
User avatar
waltervl
Posts: 6689
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2025.1
Location: NL
Contact:

Re: How to get the zwave device status from MQTT in a python script

Post by waltervl »

It is standard behavior of Domoticz that if a device is updated without a battery level indication it is set to 255 (no battery).
So perhaps the Domoticz MQTT Autodiscover integration should be modified that always a battery level is sent with the device update.
Perhaps share your expierence in the github issue.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest