Turn off switch after being on x minutes  [Solved]

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

Moderator: leecollings

Post Reply
peatmoss
Posts: 7
Joined: Wednesday 11 December 2019 23:06
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Turn off switch after being on x minutes

Post 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
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Turn off switch after being on x minutes

Post 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
peatmoss
Posts: 7
Joined: Wednesday 11 December 2019 23:06
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Turn off switch after being on x minutes

Post by peatmoss »

Thanks Waaren! I'll give this a try.
peatmoss
Posts: 7
Joined: Wednesday 11 December 2019 23:06
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Turn off switch after being on x minutes

Post 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
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Turn off switch after being on x minutes

Post 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.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
Treve
Posts: 107
Joined: Thursday 05 November 2015 10:37
Target OS: Raspberry Pi / ODroid
Domoticz version: v4.11474
Location: Rotterdam, NL
Contact:

Re: Turn off switch after being on x minutes

Post 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
- RFXtrx433E,
- AEON Labs ZW090 Z-Stick Gen5 EU
- Hue v2.1
- Raspberry Pi 3 Model B, Raspbian Stretch Full on USB-Stick.
- Domoticz 4.11474
Devices: KaKu, Z-Wave, Hue.

for testing:
Raspberry 4, 2GB, SSD
Domoticz 2022.1
Ikea Hub, Fyrtur curtain
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Turn off switch after being on x minutes

Post 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.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
peatmoss
Posts: 7
Joined: Wednesday 11 December 2019 23:06
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Turn off switch after being on x minutes

Post 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.
peatmoss
Posts: 7
Joined: Wednesday 11 December 2019 23:06
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

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

Post 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
}
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest