Page 1 of 1
setting battery level with API/JSON and/or MQTT
Posted: Tuesday 03 October 2023 16:15
by edgarhildering
My request is simple: I would like to set the battery level with an MQTT message. Example:
topic= Domoticz/in
message = { "idx" : <idx>, "BatteryLevel" : <blevel> }
with 0 <= blevel <= 100
a comparable JSON approach would be:
/json.htm?type=command¶m=udevice&idx=IDX&batterylevel=<BATTERLYLEVEL>
Furthermore it is not desirable that updates of a device that do not have battery level data force the battery level to 255. Initially this is fine, but don't touch it if there is no data.
thanks in advance, --Edgar
Re: setting battery level with API/JSON and/or MQTT
Posted: Tuesday 03 October 2023 16:44
by waltervl
Did you already try to add something as below in the update device command?
see also
https://www.domoticz.com/wiki/MQTT#MQTT_to_Domoticz
API commands are already there:
https://www.domoticz.com/wiki/Domoticz_ ... y_level.29
So what exactly are you missing?
Re: setting battery level with API/JSON and/or MQTT
Posted: Tuesday 03 October 2023 18:47
by edgarhildering
after sending:
Code: Select all
mosquitto_pub -h localhost -p 1883 -t "domoticz/in" -m "{\"idx\": 1402, \"BatteryLevel\":75}"
I get this response
Code: Select all
2023-10-03 18:43:57.259 Error: ge-MQTT: Invalid/Unhandled data received! (Topic: domoticz/in, Message: {"idx": 1402, "BatteryLevel":75})
open for MQTT-suggestions!
Re: setting battery level with API/JSON and/or MQTT
Posted: Tuesday 03 October 2023 19:03
by waltervl
Try using
Code: Select all
mosquitto_pub -h localhost -p 1883 -t "domoticz/in" -m "{\"idx\": 1402, \"Battery\":75}"
But as like the API Battery and RSSI are optional parameters for an device update command you perhaps have to add a current nvalue or svalue to it.
Re: setting battery level with API/JSON and/or MQTT
Posted: Tuesday 03 October 2023 19:07
by FireWizard
Hello @edgarhildering,
It is not "BatteryLevel", but just "Battery".
Try this one.
Regards
Re: setting battery level with API/JSON and/or MQTT
Posted: Tuesday 03 October 2023 19:29
by edgarhildering
Code: Select all
2023-10-03 19:27:35.669 Error: ge-MQTT: Invalid/Unhandled data received! (Topic: domoticz/in, Message: {"idx": 1402, "Battery":75})
That doesn't look better... I also tried:
Code: Select all
http://ge-domoticz/json.htm?type=command¶m=udevice&idx=1402&batterylevel=75
http://ge-domoticz/json.htm?type=command¶m=udevice&idx=1402&battery=75
respons in both cases:
Re: setting battery level with API/JSON and/or MQTT
Posted: Tuesday 03 October 2023 22:51
by waltervl
As mentioned before, like on the api Battery is an additional parameter so you cannot set the battery level only. You have to update the sensor with a valid value AND update the battery level.
So while updating your sensor, add the battery parameter to the statement where you normally update your sensor value with. And "BatteryLevel" will never work so forget it please.
Re: setting battery level with API/JSON and/or MQTT
Posted: Wednesday 04 October 2023 20:08
by edgarhildering
Hi @waltervl, thanks for the explanation. This works like charm when sending a message with MQTT with all other parameters present.
Then there is no need to add this feature.
I must have overlooked the remark on additional parameters. Maybe the header of that section in the wiki needs 'some improvement'. Furthermore, some explanation of the term 'the above commands' is not an unnecessary luxury, as is an example of an update of a sensor. I'll see what I can do about that.
Thanks & greetings, --Edgar
Re: setting battery level with API/JSON and/or MQTT
Posted: Wednesday 04 October 2023 21:17
by waltervl
Glad it works, I will add it too to the mqtt wiki.