Page 1 of 1

MQTT - battery percentage level without themperature update?

Posted: Tuesday 21 April 2020 20:08
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

Re: MQTT - battery percentage level without themperature update?

Posted: Tuesday 21 April 2020 23:32
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
    
}

Re: MQTT - battery percentage level without themperature update?

Posted: Wednesday 22 April 2020 0:47
by tomes
Ok, thank You again. I`ll test it.

Re: MQTT - battery percentage level without themperature update?

Posted: Wednesday 22 April 2020 13:29
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;

Re: MQTT - battery percentage level without themperature update?

Posted: Wednesday 22 April 2020 16:51
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.

Re: MQTT - battery percentage level without themperature update?

Posted: Wednesday 22 April 2020 17:28
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.

Re: MQTT - battery percentage level without themperature update?

Posted: Wednesday 22 April 2020 17:50
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.

Re: MQTT - battery percentage level without themperature update?

Posted: Wednesday 22 April 2020 18:54
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).