domoticz.devices(263).lastUpdate.minutesAgo not reliable every time?

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

Moderator: leecollings

Post Reply
Clemen
Posts: 75
Joined: Saturday 05 December 2015 15:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Europe
Contact:

domoticz.devices(263).lastUpdate.minutesAgo not reliable every time?

Post by Clemen »

HI,

I have a funny problem.

I have a PIR sensor, that turns on the light.

Some time it works and sometime it dosen't

Code: Select all

local lux_device = 265

return {
    active = true,  
	on = {
	        devices = {
	            263
	        }
	    },
  
    execute = function(domoticz, mySensor)
		
		domoticz.log('Start af script' , domoticz.PRIORITY_INFO)
		domoticz.log('Last update:' , domoticz.PRIORITY_INFO)
		domoticz.log(domoticz.devices(263).lastUpdate.minutesAgo , domoticz.PRIORITY_INFO)
		domoticz.log('263 State:' , domoticz.PRIORITY_INFO)
		domoticz.log(domoticz.devices(263).state , domoticz.PRIORITY_INFO)
		domoticz.log('175 State:' , domoticz.PRIORITY_INFO)
		domoticz.log(domoticz.devices(175).state , domoticz.PRIORITY_INFO)
		
	    if (domoticz.devices(263).lastUpdate.minutesAgo<1 and domoticz.devices(263).state == 'On' and domoticz.devices(175).state == 'Off') then
		 	domoticz.devices(175).switchOn()
			domoticz.devices(175).dimTo(30)
		 	domoticz.devices(175).dimTo(50).afterSec(2)
		 	domoticz.devices(175).dimTo(100).afterSec(4)
           
		   domoticz.log('light on' , domoticz.PRIORITY_INFO)
        end
		
		
        if (domoticz.devices(175).lastUpdate.minutesAgo>15 and domoticz.devices(263).state == 'Off' and domoticz.devices(175).state == 'On') then
            domoticz.devices(175).switchOff()
           
		   domoticz.log('light off' , domoticz.PRIORITY_INFO)
	   end
	   
	   domoticz.log('Slut af script' , domoticz.PRIORITY_INFO)

    end
}
This is my code.

The 263 is the pir sensor and trigger of the script.

When its not working the lastupdate.minutesAgo is not updating even that the 263 device is the trigger and goes from Off to On.

Code: Select all

2017-10-23 20:41:47.674 dzVents: Info: Handling events for: "BadStort | Sensor", value: "On"
2017-10-23 20:41:47.674 dzVents: Info: ------ Start external script: badstort_pir_light.lua: Device: "BadStort | Sensor (PiTeknikskab)", Index: 263
2017-10-23 20:41:47.674 dzVents: Info: Start af script
2017-10-23 20:41:47.674 dzVents: Info: Last update:
2017-10-23 20:41:47.674 dzVents: Info: 3
2017-10-23 20:41:47.674 dzVents: Info: 263 State:
2017-10-23 20:41:47.674 dzVents: Info: On
2017-10-23 20:41:47.674 dzVents: Info: 175 State:
2017-10-23 20:41:47.674 dzVents: Info: Off
2017-10-23 20:41:47.674 dzVents: Info: Slut af script
2017-10-23 20:41:47.674 dzVents: Info: ------ Finished badstort_pir_light.lua
Notice that the last update returns 3 but should be 0.
2017-10-23 20:41:47.674 dzVents: Info: Last update:
2017-10-23 20:41:47.674 dzVents: Info: 3

And of cause when its working the minutes ago are updating correctly.

Code: Select all

2017-10-23 20:34:26.125 dzVents: Info: Handling events for: "BadStort | Sensor", value: "On"
2017-10-23 20:34:26.125 dzVents: Info: ------ Start external script: badstort_pir_light.lua: Device: "BadStort | Sensor (PiTeknikskab)", Index: 263
2017-10-23 20:34:26.125 dzVents: Info: Start af script
2017-10-23 20:34:26.125 dzVents: Info: Last update:
2017-10-23 20:34:26.125 dzVents: Info: 0
2017-10-23 20:34:26.125 dzVents: Info: 263 State:
2017-10-23 20:34:26.125 dzVents: Info: On
2017-10-23 20:34:26.125 dzVents: Info: 175 State:
2017-10-23 20:34:26.125 dzVents: Info: Off
2017-10-23 20:34:26.125 dzVents: Info: light on
2017-10-23 20:34:26.125 dzVents: Info: Slut af script
2017-10-23 20:34:26.125 dzVents: Info: ------ Finished badstort_pir_light.lua
I am running on beta 3.8590

The device is correctly changing the status
Screen Shot 2017-10-23 at 20.47.09.png
Screen Shot 2017-10-23 at 20.47.09.png (34.31 KiB) Viewed 1532 times
Any ideas?
Clemen
Posts: 75
Joined: Saturday 05 December 2015 15:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Europe
Contact:

Re: domoticz.devices(263).lastUpdate.minutesAgo not reliable every time?

Post by Clemen »

The second part

" if (domoticz.devices(175).lastUpdate.minutesAgo>15 and domoticz.devices(263).state == 'Off' and domoticz.devices(175).state == 'On') then
domoticz.devices(175).switchOff()"

Is because i normally have a time trigger also.

timer = {
'every other minute'}
},

But it was removed in this script to find the error.
Clemen
Posts: 75
Joined: Saturday 05 December 2015 15:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Europe
Contact:

Re: domoticz.devices(263).lastUpdate.minutesAgo not reliable every time?

Post by Clemen »

Anyone?
leby
Posts: 98
Joined: Monday 28 July 2014 9:58
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Stockholm
Contact:

Re: domoticz.devices(263).lastUpdate.minutesAgo not reliable every time?

Post by leby »

I'm really not an expert but do you need the update at all?

Code: Select all

return {
    active = true,  
	on = {
	        devices = {
	        263
	        }
	        timer = {
	        'every minute'
	        }
	    },
  
	execute = function(domoticz, device)
		
	    if domoticz.devices(263).state == 'On' and and domoticz.devices(175).state == 'Off' then
			domoticz.devices(175).dimTo(30)
		 	domoticz.devices(175).dimTo(50).afterSec(2)
		 	domoticz.devices(175).dimTo(100).afterSec(4)
			
        elseif domoticz.devices(175).lastUpdate.minutesAgo > 15 and domoticz.devices(263).state == 'Off' and domoticz.devices(175).state  ~= 'Off') then
            domoticz.devices(175).switchOff()

	   end

    end
}
?
/lennart
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: domoticz.devices(263).lastUpdate.minutesAgo not reliable every time?

Post by dannybloe »

Note that lastUpdate holds the time of the previous update. So if you have a timer script that checks the lastUpdate of a device you will get the time when that device was last updated.

If however you have a trigger for a device (let's say a light switch) and someone presses the switch, lastUpdate will be the previous state change for that switch NOT THE CURRENT that triggered the script. That way you can easily calculated how long it has been between the current event and the previous.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
svde
Posts: 28
Joined: Sunday 17 April 2016 10:36
Target OS: Linux
Domoticz version:
Contact:

Re: domoticz.devices(263).lastUpdate.minutesAgo not reliable every time?

Post by svde »

If however you have a trigger for a device (let's say a light switch) and someone presses the switch, lastUpdate will be the previous state change for that switch NOT THE CURRENT that triggered the script. That way you can easily calculated how long it has been between the current event and the previous.
I'm also using triggers, I'm seeing that the state of the device has indeed changed, but the lastUpdate value hasn't. I expected different behavior, that the lastUpdate would be updated with a state change. I don't see anything in the logDevice output.



I've written a thermostat script in dzVents. This script is suposed to allow at least 4 minutes for the underfloor heating valves (r_lr) to open before the heatpump (hph) is turned on:

Code: Select all

if wt_lr.temperature < (h_lr.setPoint - hysteresis) or p_lr < (h_lr.setPoint - hysteresis) then
        if r_lr.state == 'Off' then
                print("### r_lr.switchOn() ###")
                r_lr.switchOn()
        elseif r_lr.lastUpdate.minutesAgo > 4 and hph.state == 'Off' then
                print("### lr hph.switchOn() ###")
                hph.switchOn()
        end
end
This is part of the logging where r_lr is switched on:

2017-11-16 13:00:01.141 dzVents: r_lr.state
2017-11-16 13:00:01.141 dzVents: Off
2017-11-16 13:00:01.141 dzVents: r_lr.lastUpdate.minutesAgo
2017-11-16 13:00:01.141 dzVents: 893
2017-11-16 13:00:01.142 dzVents: ### r_lr.switchOn() ###
2017-11-16 13:00:01.144 dzVents: Info: ------ Finished hc.lua
2017-11-16 13:00:01.145 EventSystem: Script event triggered: /opt/domoticz/scripts/dzVents/runtime/dzVents.lua

Changes to the state of r_lr and hph cause the script to be triggered again. The logging shows r_lr.state is On (correct), but the r_lr.lastUpdate.minutesAgo hasn't been reset to 0:

2017-11-16 13:00:01.511 dzVents: Info: Handling events for: "Floor livingroom", value: "On"
2017-11-16 13:00:01.521 dzVents: r_lr.state
2017-11-16 13:00:01.521 dzVents: On
2017-11-16 13:00:01.521 dzVents: r_lr.lastUpdate.minutesAgo
2017-11-16 13:00:01.521 dzVents: 893

As r_lr.state is On and r_lr.lastUpdate.minutesAgo > 4, hph is switched on. The problem is that the valves didn't get their 4 minutes to open.

A little bit later the counter is reset:

2017-11-16 13:01:30.681 dzVents: r_lr.state
2017-11-16 13:01:30.681 dzVents: On
2017-11-16 13:01:30.681 dzVents: r_lr.lastUpdate.minutesAgo
2017-11-16 13:01:30.681 dzVents: 1

I would expect that r_lr.lastUpdate.minutesAgo s reset when r_lr.state is turned on, and is reported that way when the dzVents script is triggered.


I've also tried using secondsAgo, but the behavior is the identical.

Next I tried switcing hph on afterMin(4):

Code: Select all

if wt_lr.temperature < (h_lr.setPoint - hysteresis) or p_lr < (h_lr.setPoint - hysteresis) then
        if r_lr.state == 'Off' then
                print("### r_lr.switchOn() ###")
                r_lr.switchOn()
        elseif hph.state == 'Off' then
                print("### lr hph.switchOn().afterMin(4) ###")
                hph.switchOn().afterMin(4)
        end
end
The script is triggered by a number of devices including 3 thermometers which report their temperature once every 10 minutes. Every time the script is triggered the afterMin(4) counter is reset. It often takes > 20 minutes before the heatpump is actually running.

I'm now wondering how I'm supposed to do this. Do I need to forget triggers on devices and go back to letting the script run every minute?
svde
Posts: 28
Joined: Sunday 17 April 2016 10:36
Target OS: Linux
Domoticz version:
Contact:

Re: domoticz.devices(263).lastUpdate.minutesAgo not reliable every time?

Post by svde »

A workaround for my case could be to check if the changed state is false. When changed == true, the lastUpdate.minutesAgo value hasn't been updated.

elseif r_lr.changed == 'false' and r_lr.lastUpdate.minutesAgo > 4 and hph.state == 'Off' then

Seems the changed state can't be used that way?
svde
Posts: 28
Joined: Sunday 17 April 2016 10:36
Target OS: Linux
Domoticz version:
Contact:

Re: domoticz.devices(263).lastUpdate.minutesAgo not reliable every time?

Post by svde »

Is there a way to see if an switchOn / switchOff is scheduled for a device? Like when this is done:

hph.switchOn().afterMin(4)

Can I see that the hph device will turn off within 4 minutes, so it doesn't need to be requested again?
svde
Posts: 28
Joined: Sunday 17 April 2016 10:36
Target OS: Linux
Domoticz version:
Contact:

Re: domoticz.devices(263).lastUpdate.minutesAgo not reliable every time?

Post by svde »

Fixed it by adding a persistent variable:

Code: Select all

hph_delayed_on = { initial = 0 }
Resetting it to 0 when the heatpump is turned on:

Code: Select all

if domoticz.data.hph_delayed_on == 1 and hph.state == 'On' then
        domoticz.data.hph_delayed_on = 0
end
and setting it to 1 when the heatpump is going to be turned on:

Code: Select all

if wt_lr.temperature < (h_lr.setPoint - hysteresis) or p_lr < (h_lr.setPoint - hysteresis) then
        if r_lr.state == 'Off' then
                print("### r_lr.switchOn() ###")
                r_lr.switchOn()
        elseif hph.state == 'Off' and domoticz.data.hph_delayed_on == 0 then
                domoticz.data.hph_delayed_on = 1
                print("### lr hph.switchOn().afterMin(4) ###")
                hph.switchOn().afterMin(4)
        end
end
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest