Set a switch ON that is already ON & update last seen time?

Moderator: leecollings

Post Reply
malarcy
Posts: 50
Joined: Sunday 15 November 2015 13:36
Target OS: Linux
Domoticz version:
Contact:

Set a switch ON that is already ON & update last seen time?

Post by malarcy »

Hi - I am implementing a heartbeat feature for a sensor so I know if its still doing its thing

Here's how I have it setup

It presents two sensors - for testing I have them both as "lights" - domoticz can see them and I can set them from the sensor - all good.

What I want to do is for one of the sensors to send an ON every <interval> - 30 seconds in my testing, the idea being I set an off delay on the domoticz switch of (say) 90 seconds - then if the sensors doesn't set the on for three consecutive 30 sec intervals - the switch goes OFF and the OFF notification fires.......

In my testing it seems that the gateway part of the mysensors setup does not set something ON if it thinks its already ON (or if it does domoticz doesn't take action on it) - nor does it update the Last Seen time - the effect is the the 90 secs off delay fires - the notification runs - the sensor sends it's next heartbeat - switch turns on - cycle repeats.

If I take away the delayed off and tell the heartbeat to set switch off and on again - everything happens as it should - every 30 secs log shows two actions for that switch - if I take out the off - thus trying to force ON (updating the last seen time, and effectively restarting the off delay timer)

Long winded way of saying - does sending ON to an already ON device have any effect, I thought it did for a motion sensor in the limited testing I did - is that specific to that sensor type?

Hope that makes sense - and happy to hear of any other ways of having a sensor send me " i am alive " pings - the real things it is monitoring only change every day or so - but I want to know that it's still watching. In real world usage - the heartbeats will probably be every hour - and 6 missed ones means go look at sensor.
jannl
Posts: 625
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Geleen
Contact:

Re: Set a switch ON that is already ON & update last seen ti

Post by jannl »

Not sure sensors you mean, I suppose pirs. When you knpw the time they normally are activated you could check the last updated time. When this is too long ago you can send yourself a pushover message. This is what I do with some temp sensors. My pirs are so often activated I will get complains from my wife that the light does not switch on, so no further action needed here.
malarcy
Posts: 50
Joined: Sunday 15 November 2015 13:36
Target OS: Linux
Domoticz version:
Contact:

Re: Set a switch ON that is already ON & update last seen ti

Post by malarcy »

Hi.

Issue is that in this case the last real change time for the sensors can be 24 or more hours ago and there isn't a set pattern on when they will get tripped.

The sensors in question are actually going to be connected to my house alarm. I have outputs on the panel (relays) that are normally open. When the alarm is armed one relay is closed. If/when the alarm trips the other relay closes. I have them set as "s_door". The issue is the panel is hidden away out of site and I need a battery powered sensor in there. I could use the battery level stuff but I really just need to know the sensor is still working. So I implement a 3rd sensor. I learnt an s_light. In the sensors logic I sleep waiting on an interrupt from either of the two relay connected devices and also on a timer when the loop is woken up ( for whatever reason ) I send "on" to the light sensor (the heartbeat ). I then check old and new states for both relay connected pins and if different send an update for them.

So 24 hours could go by without the alarm being armed. But when it is I want to know so I can start doing things like random light on / off sequences and playing talk radio station in the house etc. As well as turning off some stuff we only ever use when we are home.

The heartbeat is supposed to tell me the sensor has gone missing rather than noticing that it's not working I get told it's offline.

I noticed in another thread some discussions around a 1.6 mysensors feature called I_HEARTBEAT need to do more research but that might be promising.


Sent from my iPhone using Tapatalk
malarcy
Posts: 50
Joined: Sunday 15 November 2015 13:36
Target OS: Linux
Domoticz version:
Contact:

Re: Set a switch ON that is already ON & update last seen ti

Post by malarcy »

Anyone know if domoticz will only update a switch if the state changes - i.e. nothing happens if a sensor/switch that is already on gets set on again?
hahi
Posts: 12
Joined: Wednesday 08 April 2015 18:58
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Set a switch ON that is already ON & update last seen ti

Post by hahi »

I am not quite sure I follow the issue but I have a pro mini wired into my alarm panel to read the state of my reed switches. What I did was forced my sketch to send the current state at a set interval of all pins connected to my sensor.
malarcy
Posts: 50
Joined: Sunday 15 November 2015 13:36
Target OS: Linux
Domoticz version:
Contact:

Re: Set a switch ON that is already ON & update last seen ti

Post by malarcy »

My issue is that when I send the state of all the pins and nothing has changed since the last send then nothing happens on the domo side.

By nothing I mean

Last seen date and time is not updated. And any delayed off (which I am using for the heartbeat notification ) is not restated so the heartbeat "fails".

If I toggle the state in the code ( always send off then on as a test ) then everything updates.

It appears that if switch is on in domoticz and then you send it on again then that update is ignored.
hahi
Posts: 12
Joined: Wednesday 08 April 2015 18:58
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Set a switch ON that is already ON & update last seen ti

Post by hahi »

I am running build 3774 and I can confirm the last seen date is updated when a new message is received regardless if that state changed. I am using the TimeAlarms library to send the state info every 30 minutes.

Code: Select all


Alarm.timerRepeat(1800, updateState); // update relay status every 30 mins


void updateState(){
  Serial.println("Start state info");
   for (int i=0; i<NUMBER_OF_RELAYS;i++) {
      gw.sendVariable( RELAY_1+i, V_LIGHT, digitalRead(RELAY_1+i)==1?0:1 );
  }
}
malarcy
Posts: 50
Joined: Sunday 15 November 2015 13:36
Target OS: Linux
Domoticz version:
Contact:

Re: Set a switch ON that is already ON & update last seen ti

Post by malarcy »

Interesting - that's not the behaviour I am seeing (although my heartbeat is using different timer method and is using the gw.send(.....); method - I can't see that changes anything. Its good to know that others are seeing it work as I would like.

I will check the build I am running and re-test but I am pretty sure that my last seen date/time is last time state changed, not last time any update received.
malarcy
Posts: 50
Joined: Sunday 15 November 2015 13:36
Target OS: Linux
Domoticz version:
Contact:

Re: Set a switch ON that is already ON & update last seen ti

Post by malarcy »

Going to have to agree to differ from the results others have seen - just pulled latest build (2.3796) and built new sensors on 1.5 of the mysensors library.

For me - for motion sensors and lights, sending TRIPPED or ON to a device that is already TRIPPED or ON does not affect its Last Seen time nor does it "reset" any Off delay timer.

Either I am doing something wrong (but same code works if I send off then on (ie I change state) - and if I do an off delay, and set an off notification, that fires when the device goes off - and then it comes back on again at next heatbeat - so I am comfortable my sensors are sending what I expect.

I guess its time to walk through the source code to confirm what is happening.

Of course - does not explain why others are seeing different effects - perhaps its something that was introduced in a newer version to reduce database writes but I would rather it worked differently.

Perhaps one of the devs can comment - or point me to the updateswitch logic.

Thanks
malarcy
Posts: 50
Joined: Sunday 15 November 2015 13:36
Target OS: Linux
Domoticz version:
Contact:

Re: Set a switch ON that is already ON & update last seen ti

Post by malarcy »

Replying to my own post for completeness incase someone stumbles across this looking for the same info - the UpdateSwitch method in the MysensorsBase.cpp file contains the "killer" comment;

Code: Select all

//check if we have a change, if not do not update it
I commented out this block of code:

Code: Select all

		//check if we have a change, if not do not update it
		int nvalue = atoi(result[0][1].c_str());
		if ((!bOn) && (nvalue == 0))
			return;
		if ((bOn && (nvalue != 0)))
		{
			//Check Level
			int slevel = atoi(result[0][2].c_str());
			if (slevel==level)
				return;
		}
and rebuilt - the sensors are now behaving as I would like - i.e. any update is "blindly" written - the last seen date/time get updated and the off delay timer resets on every update - I have my heartbeat how I would like it - I don't yet know if there are undesirable side effects but testing continues.
DasK
Posts: 7
Joined: Sunday 27 December 2015 13:52
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Canary islands
Contact:

Re: Set a switch ON that is already ON & update last seen ti

Post by DasK »

Same issue here. Thank you for posting the solution.
I have a motion sensor (mysensors) and domoticz only updates when there is a state change, even when my sensor send a status report every minute. So I can't notice if the sensor is not working unless I trigger it.
The problem is I running domoticz in a raspberry pi, and I cant find MysensorsBase.cpp. Some advice?
malarcy
Posts: 50
Joined: Sunday 15 November 2015 13:36
Target OS: Linux
Domoticz version:
Contact:

Re: Set a switch ON that is already ON & update last seen ti

Post by malarcy »

The MySensorsBase.cpp is part of the source code - if you have not built the software from source you won't have the file (building from source isn't something that "right" for everyone). Basically - my solution involves changing the source code and then making my own "special" version of the my sensors part of domoticz.

I am not running on a PI so I can't even send you my version of the resulting file - so unless someone on a PI who is building from source could do this, or we get it considered as a feature request to be part of the main code then I don't know how you can get to the same point as me. Sorry.

I have raised it as both a bug (but it isn't really a bug - it's just the way it works) - and also as a feature request


Feature request
tracker.php?p=2&t=673

Bug report
tracker.php?p=1&t=659

Sorry I can't be any more help.
DasK
Posts: 7
Joined: Sunday 27 December 2015 13:52
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Canary islands
Contact:

Re: Set a switch ON that is already ON & update last seen ti

Post by DasK »

Thanks, you helped enough.
I realize this is made on propose, as we can see from the code comment, but then the sensor info should say "Last change: xxx" and not "Last seen: xxx" because that value is wrong. So it looks like a bug to me.
Anyway, I achieve what I wanted modifying the sensor sketch and adding a dummy distance sensor that reports "distance" every 10 minutes (it actually sends battery level instead) and with this sensor domoticz works like it suppose to do: when it doesn't receive anything for a while, the sensor label turns red, indicating something is wrong.
Now I only have to figure out how to send a notification when this happens.
malarcy
Posts: 50
Joined: Sunday 15 November 2015 13:36
Target OS: Linux
Domoticz version:
Contact:

Re: Set a switch ON that is already ON & update last seen ti

Post by malarcy »

For notification you could run a script that checks last seen time - there are a few people describing that on the forums.

Personally - I don't like to resort to scripts to do these things - I think there may be issues with some of the time based lua scripts (because they all run every minute) - so I am trying to keep my "internal" scripts to a minimum (I have zero at the moment!).

In my case - I only have one sensor (at the moment) that I need to know is alive - so I have the notification on the device itself - with my mod I update a dummy sensor every 60 mins, and have an off delay on the domoticz device of 6 hours - so - if the sensor doesn't update in 6 hours - the domoticz device turns off and i use the notification system to alert me.

Longer term my plan is an "external" script - probably python in my case because I have some experience with that language - I plan to have it run from a cron job (scheduled task) every few hours and look for "all" my sensors that have a heartbeat ( I will probably group them) - and report (via email) on those that have not updated for a while.

Not at the point where I need that script yet - so haven't written it - but will share it when I do.

I also haven't pushed too hard on the bug - I see some discussions on the mysensors group here and on the corresponding forum on mysensors.org abut a heartbeat functionality being added for 1.6 - using a I_ message type (like the battery level) - so it's possible that there is a method coming that doesn't require dummy sensors and may "just work" for our requirement,
DasK
Posts: 7
Joined: Sunday 27 December 2015 13:52
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Canary islands
Contact:

Re: Set a switch ON that is already ON & update last seen ti

Post by DasK »

I already opened a new threat about how to get the notification
viewtopic.php?f=15&t=9711
but I'm looking forward to see your python script, because you're right, there is no need to check every sensor status every minute; once or twice at day is enough, so a cron task will do a perfect job. Thanks
malarcy
Posts: 50
Joined: Sunday 15 November 2015 13:36
Target OS: Linux
Domoticz version:
Contact:

Re: Set a switch ON that is already ON & update last seen ti

Post by malarcy »

I have something "basic" - its not running in cron yet - but theres no reason it will have an issue - but I have checked with some friends who are running Domoticz on a PI - and the python install there doesn't have one of the libraries I use, nor does it have the python installers (pip or easy_install) - I think either or both of them should install no problem - python is very popular on the pi - but I don't want to start recommending installation of things that might cause issues - I will share the script as it is now if anyone is interested - but - not tested on a PI or a windows box - i know that the requests library isn't there (pi) and will need to be installed (hence wanting pip or easy_install)

The script as I have written it does this - everytime it runs it gets from Domoticz all the devices, then goes through them looking for devices where the name ends in HB (for heartbeat) - for each of those devices it checks the last updated time - works out the difference between that and "now" and rounds the answer to the nearest hour. If the difference in time is more than 6 hours (easily changed in the script) - then the script notes its name.

When all the devices have been checked - if any have been noted as being "dead", it does the following - logs a message in the domoticz log saying maintenance required for the device(s) and it turns on a virtual sensor I have created called "Maintenance Required" (name isn't important - you just need the IDX of it from the devices screen) - that switch has a notification on it when its turned on - that sends email/push notification.

I was hoping that this virtual sensor wouldn't update if I set it "on" when it was already "on" - but THAT switch does run the "on" actions (which is ironic since that is why we are doing all this for the real sensors !!!!!) - so I might modify the script to check if the "Maintenance Required" switch is on before setting it again........ I don't like being nagged about things that need to be fixed - I have people in my life who do that for me!

This way you get a visual indication on the UI for maintenance and an entry in the log telling you what's "broken"

The actual sensor
Screen Shot 2016-01-03 at 21.41.33.png
Screen Shot 2016-01-03 at 21.41.33.png (82.18 KiB) Viewed 5634 times
The "Maintenance Required" device
Screen Shot 2016-01-03 at 21.29.01.png
Screen Shot 2016-01-03 at 21.29.01.png (89.77 KiB) Viewed 5634 times
The entry in the log
Screen Shot 2016-01-03 at 21.32.02.png
Screen Shot 2016-01-03 at 21.32.02.png (39.46 KiB) Viewed 5634 times
Going to tweak it a little bit more - and as I say - happy to share with the caveats of only tested on my setup, it needs the requests library - and in my case I have modified mysensors to always update my heartbeat sensors - not tried it with distance or any other kind of sensor - but it works on device name and last update time (the date format is the one I get on my system - don't know if all locales will return the same format but I can probably work out what it should be if its different for other locales.

Any interest? - let me know - and i will start another thread in the scripts section.
DasK
Posts: 7
Joined: Sunday 27 December 2015 13:52
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Canary islands
Contact:

Re: Set a switch ON that is already ON & update last seen ti

Post by DasK »

Yes, I'm interested. Please share and I'll take a look at it.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests