Auto-On, Auto-Off and Auto-OnOff

Moderator: leecollings

rrozema
Posts: 470
Joined: Thursday 26 October 2017 13:37
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Delft
Contact:

Auto-On, Auto-Off and Auto-OnOff

Post by rrozema »

I've written about my script Auto-Off before. Now I present 2 additional scripts to complete the set: Auto-On and Auto-OnOff. Using these 3 scripts I control almost all of the devices I've automated in my home.

Auto-Off is used to switch off any device after some configurable time. Optionally one can specify one or more motion sensors that will prevent the device from going off when someone is still in the room. This script can be used for many purposes: switching off lights after some time when your kids keep forgetting to switch them off. Or a light is switched on by a motion sensor, this script makes sure the light doesn't switch off while the motion sensor still shows activity. But when the room goes quiet, the light will go off.

Auto-On is used to switch on any device when another device switches on. Examples of how to use this script are for example: a motion sensor switching on one or more lights, or a fan in the bathroom or shower that switches on automatically when the light is switched on. Auto-On now supports functions that you can use to implement o.a. not switching on when it is not dark. Examples are in the script code and further down in this thread.

Auto-OnOff switches all devices in a group on or off when any one of the devices in that group gets switched. This usefull in many situations, For example: a light in a shed switches on when the door is open and off when the door closes. Or you've got lights plugged into a wall plug and you want to control them from a wall switch: simply put all device names together in one group, and all lights will follow that wall switch. Also, when you switch off one light, all lights and the wall switch will follow. Another use case: lights in a stairway: you've got a light switch at the bottom of the stairs, but when you get up, you can't switch the light off any more. The solution (other than adding a hotel-schakeling) is to add a switch up stairs that is not wired to the light. Include the switch upstairs in one group with the switch downstairs and voila: you can switch the on and off both up- and downstairs.

All of my scripts can be found in my github: https://github.com/rrozema/Domoticz-scripts
Last edited by rrozema on Thursday 16 April 2020 15:51, edited 5 times in total.
rrozema
Posts: 470
Joined: Thursday 26 October 2017 13:37
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Delft
Contact:

Re: Auto-On, Auto-Off and Auto-OnOff

Post by rrozema »

I added the option to have a lux sensor that disables a motion sensor switching on a light when there is sufficient light in the room to the Auto-On script. The latest version of Auto-On is in my github.

For this to work I have extented the allowed entries in the SETTINGS variable. Every entry in this table now allows for either a string, a table of strings or a function returning a string or a table of strings.
Like before the string represents the name of a device that must be switched On when the (motion) sensor switches on and the table of strings represents a list of device names of devices to switch On *. The new feature is the function ( domoticz, device ) ... This function takes the domoticz environment plus the device that triggered and it is expected to return either a string or a table of strings. Inside the function you can for example retrieve a lux device from the domoticz environment, to determine the light level in the room. If it is dark, return the name(s) for the light(s) to switch on, if it there is sufficient light in the room, you don't need to return anything (or return nil). The effect will be that if there is sufficient light in the room, the lights will not switch on when someone enters, but when it is (sufficiently) dark, the lights will switch on.

Ann example of a function that switches on a lamp when it is dark, but not when there is sufficient light follows:

Code: Select all

 ["Keuken: Motion"] =
            function ( domoticz, device )
                local lux_device = domoticz.devices( "Keuken: Illuminance" )
                if nil == lux_device or lux_device.lux < 200 or lux_device.timedOut then
                    return 'Keuken: Aanrecht'
                end
            end
Which is equivalent to:

Code: Select all

 ["Keuken: Motion"] =
            function ( domoticz, device )
                local lux_device = domoticz.devices( "Keuken: Illuminance" )
                if nil == lux_device or lux_device.lux < 200 or lux_device.timedOut then
                    return { 'Keuken: Aanrecht' }
                end
            end
Just like before, if you want to switch on multiple lights, have the function return a table with the names of the devices to switch:

Code: Select all

    ["Overloop 1: Motion"] =
            function( domoticz, device )
                local lux_device = domoticz.devices( "Overloop 1: Illuminance" )
                if nil == lux_device or lux_device.lux < 20 or lux_device.timedOut then
                    return {'Washok: Plafond', 'Overloop 1: Plafond'}
                end
            end
You can decide what functionality the function implements. For example, I have a fan installed in my bathroom, which I want to switch on whenever someone uses the bathroom, the light however only needs to switch on when it is dark. So this is the function for my bathroom:

Code: Select all

    ["Badkamer: Motion"] =
            function ( domoticz, device )
                local lux_device = domoticz.devices( "Badkamer: Illuminance" )
                if nil == lux_device or lux_device.lux < 50 or lux_device.timedOut then
                    return {'Badkamer: Plafond', 'Badkamer Afzuiging'}
                else
                    return 'Badkamer Afzuiging'
                end
            end
*: Please note that as always the names of the devices are case sensitive. A great trick to make sure you have the names written right is to copy-paste each name from the device screen into the script.
Last edited by rrozema on Thursday 16 April 2020 15:49, edited 2 times in total.
Geitje
Posts: 170
Joined: Monday 22 January 2018 21:52
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Auto-On, Auto-Off and Auto-OnOff

Post by Geitje »

Thanks for this. I use the Countdown plugin for this, in combination with a long blocky script. It works fine, but your script is far more easy!
Domoticz beta, on Raspberry Pi 3B, Raspian Buster
Zwave, Zigate, RFlink etc.
EddyG
Posts: 1042
Joined: Monday 02 November 2015 5:54
Target OS: -
Domoticz version:

Re: Auto-On, Auto-Off and Auto-OnOff

Post by EddyG »

Nice scripts, would love to use them.
But I already use dzga which allows me to use the description for 'commands' too, like <voicecontrol> stuff..... </voicecontrol>
Your scripts does not like that content.
It would be nice if there was a common standard to fill the description.
rrozema
Posts: 470
Joined: Thursday 26 October 2017 13:37
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Delft
Contact:

Re: Auto-On, Auto-Off and Auto-OnOff

Post by rrozema »

EddyG wrote: Monday 11 May 2020 15:03 Nice scripts, would love to use them.
But I already use dzga which allows me to use the description for 'commands' too, like <voicecontrol> stuff..... </voicecontrol>
Your scripts does not like that content.
It would be nice if there was a common standard to fill the description.
I fully agree, Domoticz should provide a better way to store additional info with a device. Preferably by some name-tag. In my scripts I tried to be as compatible as possible by using a json format and ignoring anything that is not of interest to my script, but if dzga requires you to put xml in the description, you will not be able to use both dzga and Auto-Off for one device. For devices that you don't need both at the same time for however, it could work. At least my script has no problem if some devices have xml in their description field. It will simply ignore those.

B.t.w. only Auto-Off uses the description field. Auto-On and Auto-OnOff will work perfectly together with dzga.
Sarcas
Posts: 86
Joined: Wednesday 11 October 2017 8:50
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1ß
Location: Friesland
Contact:

Re: Auto-On, Auto-Off and Auto-OnOff

Post by Sarcas »

Enjoying your scripts very much :) Finally I can replace the mess I made myself (although I had fun writing them). I hope to use your script as a base to replace the rest of my attempts...

I noticed you mention dimlevels in the Auto-off script. It is not explained how to configure them though. I fail to figure it out, I need some explanation please.

Ultimately I want to get to get to this:
Kitchen
- If the lights in the living room are on, and there is no movement in the kitchen, the lights in the kitchen are low.
- If there is movement in the kitchen, the lights go to 100%, after x minutes of no movement they revert to off or low.
- If there is movement in the kitchen when my nightmode switch is true, lights turn on to a low level (I am raiding the fridge), after x minutes of no movement they revert to off.

Livingroom
- If there is movement and it is dark, turn on the lights, unless my nightmode switch is true.
- If there is movement and it is dark and my nightmode switch is true, turn on the lights on a low level, after x minutes of no movement they revert to off.

I have lux and movement sensors in both rooms and my own scripts more or less work, but it happens a couple of times a week that we're suddenly in the dark in the kitchen, or lights go on at night and the scripts have become monsters.
--

Domoticz on rPi4 - RFXCOM RFXtrx433 USB - ZW090 Z-Stick Gen5 EU - IKEA Tradfri - Philips HUE - YouLess meter - SolarEdge
jake
Posts: 742
Joined: Saturday 30 May 2015 22:40
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

Re: Auto-On, Auto-Off and Auto-OnOff

Post by jake »


Sarcas wrote:.
Ultimately I want to get to get to this:
To stay out of trouble: either create 2 scripts, one for the living room and one for the kitchen, or have 2 very specific sections in your script to control both environments. I prefer to do the first option, it will make your life much easier, since you don't need to worry that script triggers give unexpected results.

I have a similar 'raid script':
A cabinet in the living room has a LED strip that I can control separately. It will switch on together with the other living room lights. When those lights are switched off at night, the LED strip will stay on for 5 minutes, to enable leaving the room without stumbling or the 'oopsie, I still need to get this out that from the room'. Secondly this LED strip will turn on at night on motion to prevent stumbling again during 'raid activities', or simply drinking a glass of tap water after a toilet visit.
All together very convenient.

Other reasons for unexpected script executions are:
You have not defined all states:
- what if the night switch is just turned on?
- what when the night switch is turned off?
- etc

When using dzvents, always use the double command while using 'on for x' and 'off after y' to prevent 2 timers crossing read other.

Also, be prepared for the situation that your domoticz is restarted for one or other reason while a timer is running. I always add some logic for these circumstance and I check for them every half hour or so, depending on how long timers will be set

Last of all, I'd advice to create a virtual motion detector that is controlled by the physical one. This enabled you to have a much longer ON time than you want your physical motion sensor to be on. This is very handy for your living room where you won't have motion for a longer period, while watching TV or reading a book.
rrozema
Posts: 470
Joined: Thursday 26 October 2017 13:37
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Delft
Contact:

Re: Auto-On, Auto-Off and Auto-OnOff

Post by rrozema »

I was preparing an answer that you can do some parts of your scenarios using my scripts, but also that some parts of them are just to specific for a generic solution. You did give me an idea though for an extension that might solve another part. I think I'm going to add an option to auto-on for a delayed-start function: if a trigger device switches to 'On', a timer starts for n seconds. When the timer fires and the trigger device is still 'On', only then the slave device is switched 'On', if the trigger device is 'Off' by the time the timer fires, the timer event is ignored. I think I could apply that to my home for example in the bathroom: if motion is detected, the fan needs to go on only if the person stays in the room for more than 2 minutes. I don't know when I will have the time, but I will try to add this to Auto-On.

Then on the tips jake gave you: i agree with him that it is better to have different scripts for the different devices. I have an additional tip: try to avoid combining timer and device triggers in one script. Scripts that are executed by both timers and device-state-changes tend to become very complicated very quickly. An easy way to avoid having timer and device triggers in one script is to introduce a dummy switch instead of the timer trigger. The dummy switch can be switched by a timer, but the script will not have to deal with the timer. For example, I have a dummy switch that is on when the sun is up and off when the sun is down. I know there is a timer trigger for that, but using the switch in my scripts is a lot easier than having to check for the time in every scripted action that needs the sun-is-up/down information. Plus it has more advantages:
- debugging your scenario has suddenly become easy: toggle the dummy switch and for your script(s) the sun has just gone down or up without you having to wait another day.
- changing the start or end time is also easy now: do you want the lights to go on half an hour before the sun sets? just change the one timer on the dummy switch and the script(s) will now all use the new time.
- combining timers has become easy too: I've got an additional dummy switch for when my normal bedtimes are. When we're asleep I don't want the ventilation to run at full speed, neither do I want the outside porch lights to be on. I switch my porch light based on the state of the both dummy-switches: if the sun-is-up button is off and also the sleep-time button is off, then the porch light needs to go on, if either goes on the porch lights go off. This fixes an easily made mistake: when the sun is up till very late during summer, it can happen that the sun goes down after sleep time starts. If you would trigger the light only by the timer events themselves, you could switch the light on when the sun goes down and of when sleep time starts. During a few days in summer however your lights will be on all night because the sun set after sleep time started. I'm not saying it is not possible to script this correctly using timer events, i'm saying it's easier to get it right when you use dummy switches with a timer on it instead of the timer event itself.
Ninja1283
Posts: 3
Joined: Tuesday 26 May 2020 3:35
Target OS: Linux
Domoticz version:
Contact:

Re: Auto-On, Auto-Off and Auto-OnOff

Post by Ninja1283 »

I'm just getting started w/ Domoticz scripting, and these scripts are great. Are you able to explain the formatting for the OnOff one? Am I reading it correctly that the switches and groups go in the SETTINGS array, and the triggers and groups arrays will be built by the script?
rrozema
Posts: 470
Joined: Thursday 26 October 2017 13:37
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Delft
Contact:

Re: Auto-On, Auto-Off and Auto-OnOff

Post by rrozema »

Ninja1283 wrote: Tuesday 26 May 2020 3:46 I'm just getting started w/ Domoticz scripting, and these scripts are great. Are you able to explain the formatting for the OnOff one? Am I reading it correctly that the switches and groups go in the SETTINGS array, and the triggers and groups arrays will be built by the script?
That is correct. You only need to edit the values for the SETTINGS table and like you say, the triggers and groups arrays are built by the script from that.
Ninja1283
Posts: 3
Joined: Tuesday 26 May 2020 3:35
Target OS: Linux
Domoticz version:
Contact:

Re: Auto-On, Auto-Off and Auto-OnOff

Post by Ninja1283 »

In the following format?
SETTINGS = {
{ "GROUP1: SWITCH1", "GROUP1: SWITCH2" },
{ "GROUP2: SWITCH1", "GROUP2: SWITCH2" },
{ "GROUP3: SWITCH1", "GROUP3: SWITCH2" }
}


I currently have only one group set up in the script:
SETTINGS = {
{ "GROUP1: SWITCH1", "GROUP1: SWITCH2" }
}

But I'm getting the following error:
Error: EventSystem: Lua script OnOff did not return a commandArray
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Auto-On, Auto-Off and Auto-OnOff

Post by waaren »

Ninja1283 wrote: Tuesday 26 May 2020 7:41 But I'm getting the following error:
Error: EventSystem: Lua script OnOff did not return a commandArray
You saved the script as a Lua event script and not as a dzVents.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Ninja1283
Posts: 3
Joined: Tuesday 26 May 2020 3:35
Target OS: Linux
Domoticz version:
Contact:

Re: Auto-On, Auto-Off and Auto-OnOff

Post by Ninja1283 »

waaren wrote: Tuesday 26 May 2020 9:42 You saved the script as a Lua event script and not as a dzVents.
I re-saved as dzVent, and errors went away, but the other member in the group is not triggering correctly. I don't see anything coming up in the logs. Is my formatting correct with the "GROUP: SWITCH" entries?
rrozema
Posts: 470
Joined: Thursday 26 October 2017 13:37
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Delft
Contact:

Re: Auto-On, Auto-Off and Auto-OnOff

Post by rrozema »

Ninja1283 wrote: Tuesday 26 May 2020 7:41 In the following format?

Code: Select all

SETTINGS = {
{ "GROUP1: SWITCH1", "GROUP1: SWITCH2" },
{ "GROUP2: SWITCH1", "GROUP2: SWITCH2" },
{ "GROUP3: SWITCH1", "GROUP3: SWITCH2" }
}
I currently have only one group set up in the script:

Code: Select all

SETTINGS = {
{ "GROUP1: SWITCH1", "GROUP1: SWITCH2" }
}
But I'm getting the following error:
Error: EventSystem: Lua script OnOff did not return a commandArray
As waaren already mentioned, you need to specify "dzvents" for the script type. It doesn't matter which template you choose after that: you're going to overwrite all default content with the content of my script.

No, the settings variable needs a list of lists, the 1st list is the list of groups, the 2nd list are the devices in each group. The :-signs in the device names are part of my naming convention for my devices, nothing to do with the script.

So:

Code: Select all

SETTINGS = {
    group_1,
    group_2,
    ...,
    group_n
}
where
group_x = { device_1 [, device_2[, device_n]]] }

and
device_x = "device_name"

so as an example:

Code: Select all

SETTINGS = {
   {"my 1st device", "my 2nd device"},
   {"my 3rd device", "my 4th device", "my 5th device"}
}
This will sync "my 1st device" and "my 2nd device", being the 1st group. And it will also keep all devices in the 2nd group in sync: "my 3rd device", "my 4th device" and "my 5th device".
Sarcas
Posts: 86
Joined: Wednesday 11 October 2017 8:50
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1ß
Location: Friesland
Contact:

Re: Auto-On, Auto-Off and Auto-OnOff

Post by Sarcas »

@jake & @rrozema
Thanks for the advice :)

S.
--

Domoticz on rPi4 - RFXCOM RFXtrx433 USB - ZW090 Z-Stick Gen5 EU - IKEA Tradfri - Philips HUE - YouLess meter - SolarEdge
desertdog
Posts: 84
Joined: Sunday 14 August 2016 13:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: Netherlands
Contact:

Re: Auto-On, Auto-Off and Auto-OnOff

Post by desertdog »

when activating the script I get the following errors:

Code: Select all

2020-09-24 16:51:00.518 Status: dzVents: Info: Generic Auto Off v2.03: ------ Start internal script: Auto-Off:, trigger: "every minute"
2020-09-24 16:51:00.519 Status: dzVents: Info: Generic Auto Off v2.03: Generic Auto Off v2.03, Domoticz v2020.2, Dzvents v3.0.2.
2020-09-24 16:51:00.548 Status: dzVents: Info: Generic Auto Off v2.03: Device description for Balcony Sensor is not in json format. Ignoring this device.
2020-09-24 16:51:00.680 Status: dzVents: Info: Generic Auto Off v2.03: Device description for VS 15 min timer is not in json format. Ignoring this device.
2020-09-24 16:51:00.682 Status: dzVents: Info: Generic Auto Off v2.03: Scanned 103 devices.
2020-09-24 16:51:00.682 Status: dzVents: Info: Generic Auto Off v2.03: ------ Finished Auto-Off
2020-09-24 16:51:00.714 Status: LUA: Domoticz alive !
2020-09-24 16:51:00.548 Error: dzVents: Error: (3.0.2) Generic Auto Off v2.03: Error parsing json to LUA table: /home/pi/domoticz/scripts/dzVents/../lua/JSON.lua:1234: /home/pi/domoticz/scripts/dzVents/../lua/JSON.lua:808: can't parse JSON at byte 1 of: Balcony temperature external sensor
2020-09-24 16:51:00.680 Error: dzVents: Error: (3.0.2) Generic Auto Off v2.03: Error parsing json to LUA table: /home/pi/domoticz/scripts/dzVents/../lua/JSON.lua:808: trailing garbage at byte 3 of: 15min = 900s
2020-09-24 16:51:00.680 30min = 1800s
these errors seem to come from somewhere else, as I dont see they mention 'trailing garbage' and '30min = 1800s' that doesn't appear in the script, but the error only appears when i activate the auto-off script
rrozema
Posts: 470
Joined: Thursday 26 October 2017 13:37
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Delft
Contact:

Re: Auto-On, Auto-Off and Auto-OnOff

Post by rrozema »

desertdog wrote: Thursday 24 September 2020 16:56 when activating the script I get the following errors:
...
Post the json you entered in the description of (one of) the devices. Maybe we can see what's wrong.
desertdog
Posts: 84
Joined: Sunday 14 August 2016 13:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: Netherlands
Contact:

Re: Auto-On, Auto-Off and Auto-OnOff

Post by desertdog »

This is one of them. They are all quite similar. Only the times and devices differ (devices names are copy/pasted from the devices list)

Code: Select all

{
 "auto_off_minutes": 15,
 "auto_off_motion_device": "Motion - Corridor"
 }
 
rrozema
Posts: 470
Joined: Thursday 26 October 2017 13:37
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Delft
Contact:

Re: Auto-On, Auto-Off and Auto-OnOff

Post by rrozema »

desertdog wrote: Thursday 24 September 2020 18:08 This is one of them. They are all quite similar. Only the times and devices differ (devices names are copy/pasted from the devices list)

Code: Select all

{
 "auto_off_minutes": 15,
 "auto_off_motion_device": "Motion - Corridor"
 }
 
That seems to be OK. Do you by any coincidence run your Domoticz on a NAS? I've just seen a similar strange issue in another question: the json seemed fine, but somehow the parsing went wrong.
desertdog
Posts: 84
Joined: Sunday 14 August 2016 13:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: Netherlands
Contact:

Re: Auto-On, Auto-Off and Auto-OnOff

Post by desertdog »

No, I run Domoticz on a raspberry pi
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests