update battery level on dummy switch

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

Moderator: leecollings

Post Reply
Ramond
Posts: 4
Joined: Saturday 07 September 2019 9:47
Target OS: -
Domoticz version:
Contact:

update battery level on dummy switch

Post by Ramond »

Hello everyone,,
im using dzvents to control soma shades (already works quite well), but i want to add code to read battery level and update on domoticz side (will be triggered and update value every time when i open shades), question is how to update battery level value ? (tried .batteryLevel(20)) but thats not working :)reversephonelookup.onl/ curated.onl/nba-reddit/
next problem would be to parse python script value, but that shouldnt be hard :)

Code: Select all

return {
    on = {
        devices = {576}
    },
    execute = function(domoticz, switch)
    local roleta = domoticz.devices(576)
    local mac_a = 'D6:84:F4:AD:F7:3A'
    local val_shades_on = '0'
    local val_shades_off = '100'
    local on_command = "python /home/pi/python/SOMA/control.py  -t " ..mac_a.. " -c move_target -a " ..val_shades_on
    local off_command = "python /home/pi/python/SOMA/control.py  -t " ..mac_a.. " -c move_target -a " ..val_shades_off

    domoticz.log('TEST', domoticz.LOG_FORCE)
        if (switch.state == 'Open') then
            domoticz.log('Opening....', domoticz.LOG_FORCE)
            domoticz.log(on_command, domoticz.LOG_FORCE)
            os.execute(on_command)
            -- roleta.batteryLevel(20)

        elseif  (switch.state == 'Closed') then
            domoticz.log('closing....', domoticz.LOG_FORCE)
            domoticz.log(off_command, domoticz.LOG_FORCE)
            os.execute(off_command)

        end
    end
}
Last edited by Ramond on Monday 11 November 2019 23:32, edited 1 time in total.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: update battery level on dummy switch

Post by waaren »

Ramond wrote: Thursday 07 November 2019 17:39 I want to add code to read battery level and update on domoticz side. Question is how to update battery level value ?
next problem would be to parse python script value, but that shouldnt be hard :)
There is no native dzVents command to update batteryLevel as it cannot be done in isolation. It always has to be done in combination with sValue, nValue and that causes an event.

Can you try this script ? It uses a workaround to prevent the script to execute real payload when triggered by the openURL.

Code: Select all

return {
    on = {
        devices = {576}
    },

    data = { noAction = { initial = false } },
    
    execute = function(domoticz, item)
        
        if domoticz.data.noAction then -- to prevent retrigger by the openURL update
            domoticz.data.noAction = false  
            return
        end
        
        local mac_a = 'D6:84:F4:AD:F7:3A'
        local val_shades_on = '0'
        local val_shades_off = '100'
        local on_command = "python /home/pi/python/SOMA/control.py  -t " ..mac_a.. " -c move_target -a " ..val_shades_on
        local off_command = "python /home/pi/python/SOMA/control.py  -t " ..mac_a.. " -c move_target -a " ..val_shades_off
        local batteryLevel = item.lastUpdate.secondsAgo % 100 -- Just for test use some magic python here to get the real battery level
        
        local function setBattery(state, level)
            local url = domoticz.settings['Domoticz url'] .. 
                        '/json.htm?type=command&param=udevice&svalue=0' .. 
                        '&nvalue=' .. state ..
                        '&idx=' .. item.id .. 
                        '&battery=' .. level
                        
            domoticz.openURL(url).afterSec(1)
            domoticz.data.noAction = true
        end
    
        domoticz.log('TEST', domoticz.LOG_FORCE)
        if item.state == 'Open' then
            domoticz.log('Opening....', domoticz.LOG_FORCE)
            domoticz.log(on_command, domoticz.LOG_FORCE)
            --os.execute(on_command)
            setBattery(0, batteryLevel)
        elseif item.state == 'Closed' then
            domoticz.log('closing....', domoticz.LOG_FORCE)
            domoticz.log(off_command, domoticz.LOG_FORCE)
            -- os.execute(off_command)
            setBattery(1, batteryLevel)
        end
        
    end
}


Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest