Page 1 of 1

Turn off switch after being on x minutes

Posted: Wednesday 11 December 2019 23:14
by peatmoss
Hi,

I have a hot water recirculating pump that I control with Domoticz. We turn this on 15 minutes before taking a shower of when we want hot water at the faucet. Is there a way to turn off this switch after it has ben on for x minutes? I tried doing this in dzVents without luck. Here is what I have so far, 269 is the "hot water" switch id:

Code: Select all

return {
    on = {
        devices = { 269 }
    },
    execute = function(domoticz, item)
        if domoticz.devices(269).state == 'On' then
            domoticz.devices('hot water').switchOff().afterMin(30)
            domoticz.log('hot water is on, shutting down in 30 min')
        end
    end
}
This seems to be unreliable, it only works sometimes. Nothing shows in the logs why it fails. Also there does not seem to be a variable for time device has been on. Like 269.hasBeenOnFor

Best home automation system I have used. The most reliable, flexible and easy to use!

Thanks!
Pete

Re: Turn off switch after being on x minutes

Posted: Thursday 12 December 2019 0:25
by waaren
peatmoss wrote: Wednesday 11 December 2019 23:14 Is there a way to turn off this switch after it has ben on for x minutes? I tried doing this in dzVents without luck. Here is what I have so far, 269 is the "hot water" switch id
I see nothing in your script that would prevent the hot water pump (device 269) to get switched off after 30 minutes if domoticz is not restarted in these 30 minutes.

if you use below script and inspect the logfile you should be able to see what happens.

Code: Select all

return 
{
    on = 
    {
        devices = { 269 },
        timer = {'every 5 minutes' },
    },
    
    logging = { level = domoticz.LOG_DEBUG }, -- switch to LOG_ERROR when working as expected

    execute = function(dz, item)
        _G.logMarker =  _G.moduleLabel
        
        local hotWater = dz.devices(269)
        
        if item.isDevice then
            if item.state == 'On' then
            	item.cancelQueuedCommands()
                item.switchOff().afterMin(30)
                dz.log('hot water is on, shutting down in 30 min',dz.LOG_DEBUG)
            else
                dz.log('hot water switched off',dz.LOG_DEBUG)
            end
        else
            dz.log('hot water state is ' .. hotWater.state .. '. Last update was ' .. hotWater.lastUpdate.secondsAgo .. ' seconds ago.' ,dz.LOG_DEBUG)
        end
    end
}

Re: Turn off switch after being on x minutes

Posted: Thursday 12 December 2019 1:03
by peatmoss
Thanks Waaren! I'll give this a try.

Re: Turn off switch after being on x minutes

Posted: Thursday 12 December 2019 6:53
by peatmoss
Its still not shutting off. After a closer look at the logs I think mqtt keeps updating the status and resets the off timer. I may have to fix this by setting a variable to keep track of the on time. Here are the logs:

Code: Select all

2019-12-11 21:50:14.289 (MQTTDisco) 012/hot water: Topic: 'hot_water/tele/STATE 'Setting nValue: 1->1, sValue: ''->''
2019-12-11 21:50:14.532 Status: dzVents: Info: Handling events for: "hot water", value: "On"
2019-12-11 21:50:14.532 Status: dzVents: Info: ------ Start internal script: hot-water-off: Device: "hot water (MQTTDisco)", Index: 269
2019-12-11 21:50:14.533 Status: dzVents: Debug: Constructed timed-command: Off
2019-12-11 21:50:14.533 Status: dzVents: Debug: Constructed timed-command: Off AFTER 1800 SECONDS
2019-12-11 21:50:14.533 Status: dzVents: Debug: hot water is on, shutting down in 30 min
2019-12-11 21:50:14.533 Status: dzVents: Info: ------ Finished hot-water-off
2019-12-11 21:50:14.536 Status: EventSystem: Script event triggered: /opt/domoticz/dzVents/runtime/dzVents.lua

Re: Turn off switch after being on x minutes

Posted: Thursday 12 December 2019 8:14
by waaren
peatmoss wrote: Thursday 12 December 2019 6:53 Its still not shutting off. After a closer look at the logs I think mqtt keeps updating the status and resets the off timer. I may have to fix this by setting a variable to keep track of the on time.
I included the time trigger in the script to identify this kind of external influence. If you start logging to an OS file and inspect the log you might be able to see what is setting the hot water device.

Re: Turn off switch after being on x minutes

Posted: Thursday 12 December 2019 8:20
by Treve
Image

I’m using this simpel blocky to switch off my phone charger, it’s working flawless.
I’m using Domoticz version 4.11474

Re: Turn off switch after being on x minutes

Posted: Thursday 12 December 2019 8:39
by waaren
Treve wrote: Thursday 12 December 2019 8:20 I’m using this simpel blocky to switch off my phone charger, it’s working flawless.
The issue here is not so much that the switchOff after xxx seconds is not working. The issue is that the switch state is not only controlled by the script that @peatmoss created for it but is switched on by another process, -script or mqtt. The blockly solution you propose will face that same issue.
So next step in finding the root cause is not to use another method to switch it off after some time but to identify what process, -script or mqtt is also changing the state of the hot water switch.

Re: Turn off switch after being on x minutes

Posted: Thursday 12 December 2019 20:46
by peatmoss
Yeah I should have mentioned it can be turned on by Alexa via fauxmo, the actual switch or Domoticz web. I'll look at logging to a file to debug.

Re: Turn off switch after being on x minutes  [Solved]

Posted: Thursday 12 December 2019 21:33
by peatmoss
This seems to work. I used a user variable. Not the most elegant but...

Code: Select all

return 
{
    on = 
    {
        devices = { 269 },
        timer = {'every 5 minutes' }
    },
    execute = function(domoticz, item)
        if item.isTimer then
            if domoticz.devices(269).state == 'On' then
                local hotWaterTime = domoticz.variables('hotwatertime').value
                if hotWaterTime > 5 then -- 6*5=30 min
                    domoticz.devices('hot water').switchOff()
                    domoticz.variables('hotwatertime').set(0)
                else
                    domoticz.variables('hotwatertime').set(hotWaterTime + 1)
                end
            else
                domoticz.variables('hotwatertime').set(0)
            end
        end
    end
}