MQTT - battery percentage level without themperature update?

Moderator: leecollings

Post Reply
tomes
Posts: 31
Joined: Saturday 07 July 2018 22:08
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Poland
Contact:

MQTT - battery percentage level without themperature update?

Post by tomes »

Hi,
There is a temperature sensor. I would like to update their temperature value and battery level with separate MQTT commands. Is it possible?
When I type: {"idx": 123, "Battery": 50} an error appears.
When I enter the temperature without battery, the battery level disappears.
How to update only one value without erase other?
Tom
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: MQTT - battery percentage level without themperature update?

Post by waaren »

tomes wrote: Tuesday 21 April 2020 20:08 There is a temperature sensor. I would like to update their temperature value and battery level with separate MQTT commands. Is it possible?
How to update only one value without erase other?
The only way to update a batteryLevel of a temperature sensor (besides poking directly in the database) is with the combined MQTT or JSON command.
Luckily dzVents can help here.

if you send one of these MQTT messages to domoticz/in

Code: Select all

{"command":"customevent","event":"separateValues","data": "{'"'idx'"':29, '"'temperature'"':25 }" } # for temperature only 
{"command":"customevent","event":"separateValues","data": "{'"'idx'"':29, '"'batteryLevel'"':29 }" } # for batteryLevel only
Below dzVents script will handle the update of the temperature sensor

Code: Select all

local scriptVar = 'separateValues'

return 
{
    on = 
    {
        customEvents = 
        {
            scriptVar,
        },
        httpResponses = 
        {
            scriptVar,
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- set to LOG_ERROR when tested and OK
        marker = scriptVar,
    },

    execute = function(dz, item)
        lodash = dz.utils._
        
        local function sendURL(idx, temperature, batteryLevel)
            local url = dz.settings['Domoticz url'] ..
                        '/json.htm?type=command&param=udevice&idx=' .. idx .. 
                        '&nvalue=0&svalue=' .. temperature .. 
                        '&battery=' .. batteryLevel
            dz.openURL(
            {
                url = url,
                callback = scriptVar,
            })
        end
        
        if item.isCustomEvent then
            local mqtt = item.data
            local batteryLevel = mqtt.batteryLevel or dz.devices(mqtt.idx).batteryLevel or 255
            local temperature = mqtt.temperature or dz.devices(mqtt.idx).temperature
            
            sendURL(mqtt.idx, temperature, batteryLevel)
            
        elseif not item.ok then
            dz.log('Problem with sending the temperature / batteryLevel' .. lodash.str(item), dz.LOG_ERROR)     
        else
            dz.log('All ok \n' .. lodash.str(item.data) .. '\n', dz.LOG_DEBUG)     
        end
    end
    
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
tomes
Posts: 31
Joined: Saturday 07 July 2018 22:08
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Poland
Contact:

Re: MQTT - battery percentage level without themperature update?

Post by tomes »

Ok, thank You again. I`ll test it.
thomasbaetge
Posts: 153
Joined: Wednesday 02 October 2019 11:47
Target OS: Linux
Domoticz version: 2023.1
Location: DE / BY / LT
Contact:

Re: MQTT - battery percentage level without themperature update?

Post by thomasbaetge »

Hi,

since you're working already with MQTT, this is how I update the battery value with node-red. The sample is from a flood sensor. the "svalue" : "0" is required, otherwise it won't work.

Code: Select all

msg.payload = { 
    
    "idx" :290,
     "svalue" : "0",
     "Battery" :parseInt(msg.payload)
         
     
}
return msg;
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: MQTT - battery percentage level without themperature update?

Post by waaren »

thomasbaetge wrote: Wednesday 22 April 2020 13:29 since you're working already with MQTT, this is how I update the battery value with node-red. The sample is from a flood sensor. the "svalue" : "0" is required, otherwise it won't work.
if you send svalue 0 to a temperature sensor it will show a temperature of 0 degrees.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
thomasbaetge
Posts: 153
Joined: Wednesday 02 October 2019 11:47
Target OS: Linux
Domoticz version: 2023.1
Location: DE / BY / LT
Contact:

Re: MQTT - battery percentage level without themperature update?

Post by thomasbaetge »

Right...but I assume NVALUE=0 will do the trick too.
The point is, that just sending the Battery value doesn't seem to work.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: MQTT - battery percentage level without themperature update?

Post by waaren »

thomasbaetge wrote: Wednesday 22 April 2020 17:28 Right...but I assume NVALUE=0 will do the trick too.
The point is, that just sending the Battery value doesn't seem to work.
That's why I proposed the dzVents approach which will preserve the current value for temperature when updating the battery-value and preserving the battery-value when updating the temperature.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: MQTT - battery percentage level without themperature update?

Post by ben53252642 »

What I do is when a device only reports its battery level I store it in Node-Red and then when a temperature level is reported I also send the previously received battery reading (or latest if its available).

You can code a rule if you want to only send the battery reading with temperature if the battery reading was received within the last 2 days (so that its reasonably current, obviously depending on the power consumption of the device).
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest