Page 1 of 1

update battery level on dummy switch

Posted: Thursday 07 November 2019 17:39
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
}

Re: update battery level on dummy switch

Posted: Thursday 07 November 2019 20:20
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
}