Could someone take a look at this code please.

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
Dave21w
Posts: 431
Joined: Sunday 29 November 2015 21:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: UK
Contact:

Could someone take a look at this code please.

Post by Dave21w »

I have a few sensors that should run on 2 x AAA batteries (alkaline) so obvisously the start voltage is 3v or a bit more. These zigbee sensors don't have a separate widget in the utility tab for battery percentage or voltage so I either need to look at the device in the device list or rely on the low batt warning set point of 20% which is fine for all my other devices. As I'm actually running these sensors on a pair of NiMh cells the initial voltage is high enough to see 100% state but quickly drops to 75%, due to the discharge curve of NiMh cells it stays here for quite a while until suddenly dropping to about 30%. I want to get a warning at this point so added a dummy battery device which I update with this script, I pull the battery state from the device IDX and put it in my dummy sensor, from there I can monitor it how I wish. This was working absolutely fine until about 3 or 4 weeks ago, I'm not sure which beta update point it was but it started to not update or update randomly. Currently the update rate is set to every 5 mins so i can try and see how long it takes before it reads a correct value.

Code: Select all

return {
    on = {
        timer = { 'every 5 minutes' } -- This script will periodically update the battery level
    },
    execute = function(domoticz)
        -- Fetch the battery level of the device from its IDX
        local device = domoticz.devices(1234) -- IDX Device no.
        local batteryLevel = device.batteryLevel
        
        -- Virtual device IDX to display the battery state
        local virtualDevice = domoticz.devices(1301)
        if virtualDevice then
            virtualDevice.updateCustomSensor(batteryLevel)
        else
            domoticz.log('Utility device "Battery Status" not found!', domoticz.LOG_ERROR)
        end
    end
}

A prime example is this moring I noticed the batteries are low in this device, I replaced them with fully charged ones before 8am. Z2M says battery is 100%, the Domoticz device says it's 100% but even though my script is showing no errors and running every 5 mins my dummy device stills says the battery is at 5% and its now 8:20am, at some point today it will update but it should have done so already. When the script runs you can see the widget blink so I know its being written to, I just don't understand where it is getting the old value from.

Thanks
User avatar
waltervl
Posts: 7020
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2026.3
Location: NL
Contact:

Re: Could someone take a look at this code please.

Post by waltervl »

You know you can set a general low battery level, in your case 30 (%) in menu Setup, Settings, tab Other:
Battery Low Level
Here you can set the level at which you get a notification that the battery level of the device is low. 0 = no notification.
A device widget will become yellow to indicate it is below set battery level.
There are also example scripts if you want to test on a separate level: https://wiki.domoticz.com/Battery_level_check
I do not think there is a need to create dummy batterylevel devices for this.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Dave21w
Posts: 431
Joined: Sunday 29 November 2015 21:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: UK
Contact:

Re: Could someone take a look at this code please.

Post by Dave21w »

As I said, I'm aware of the battery level in settings, that is set to 20% which is fine for all other devices, I need to detect when these particular sensors hit 30% and this code worked fine until a few weeks ago. The level actually updated at 4:30pm yesterday despite being read every 5 mins from 8am when I changed the batteries which doesn't make any sense.
User avatar
boum
Posts: 137
Joined: Friday 18 January 2019 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: Could someone take a look at this code please.

Post by boum »

maybe the sensor is just sending battery update only once a day. not with every measure update. some payload/battery optimization perhaps
does the battery level on the devices page change more often?
Dave21w
Posts: 431
Joined: Sunday 29 November 2015 21:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: UK
Contact:

Re: Could someone take a look at this code please.

Post by Dave21w »

Hey thanks for the reply, the battery level in Z2M and the level shown on the devices page changes as soon as fresh batteries are inserted but my widget seems to get updated when it feeld like it even though as I said the widget blinks every 5 mins (my current update rate) so it shoul dbe updated. I'm pulling the battery level from the correct device IDX so it really is a puzzle. Because NiMh cells go from being OK to discharged very quickly I just need some advanced warning before the current setpoint of 20% for everything else.
User avatar
RonkA
Posts: 142
Joined: Tuesday 14 June 2022 12:57
Target OS: NAS (Synology & others)
Domoticz version: 2025.1
Location: Harlingen
Contact:

Re: Could someone take a look at this code please.

Post by RonkA »

Wikipedia states:
NiMH batteries are typically used as a substitute for similarly shaped non-rechargeable alkaline and other primary batteries. They provide a cell voltage of about 1.2V while fresh alkaline cells provide 1.5V; however devices designed for alkaline batteries operate until cell voltage gradually drops to around 1.0V, while the voltage of a fully-charged NiMH cell drops more slowly, giving good endurance for a 1.0V end point.
So your startvoltage for new NiMH batteries is the voltage that alkaline batteries are reaching EOL thus giving a low battery indication.

The resolution for the NiMH battery voltage drop indication is very low compared by alcaline ones making it difficult to determine a usable value:
1,1008 Volt van be 70% and 1,1002 Volt can be 40%.. also you measure 2 batteries at once so there can be added difference because of this..

In my opinion if you really want to be sure all is working just set a timer for a few months and recharge the batteries..
SolarEdge ModbusTCP - Kaku - Synology NAS - Watermeter - ESPEasy - DS18b20
Work in progress = Life in general..
Dave21w
Posts: 431
Joined: Sunday 29 November 2015 21:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: UK
Contact:

Re: Could someone take a look at this code please.

Post by Dave21w »

I appreciate your response however I'm not concerned about Wikipedia's definition of a NiMH cell, I've been a communcations and industrial electronics engineer for over 30 years, I fully understand the discharge curve of different cell technologies. I just want to know why my script read the battery voltage / percentage perfectly up to about a month ago and now doesn't seem to work anymore. The script produces no errors, definitely updates the widget value but not with the current correct battery state. Even if i put Alkalines in the device I should see the value slowly decrease but this no longer happens, it just seems to grab a value a random times depsite running very regularly, when putting fresh cells in the value in Z2M and the battery icon on the devices page both show 100% but my script still seems to load the old value into the widget for quite some time then at some random point it will be correct.
Post Reply