Heating Script struggling to understand

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

Moderator: leecollings

Post Reply
digital
Posts: 12
Joined: Tuesday 12 December 2017 16:16
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Contact:

Heating Script struggling to understand

Post by digital »

I have tried dzVents a while ago and did not have much luck. My fault just my lack of understanding.
So I thought as I can now control my Evohome through Domoticz I would have another go.
I want the heating to switch from Auto to off if the outside Temperature rises above a certain level.
I have managed to get this working with the script below. I only want this script running between 09:00-16:00 and need some means of stopping the script running so I can control my Evohome normally. I created a dummy Switch called 'Heating Auto' and hoped I could get the script to run when this switch was 'On" and between the above times. I just seem to be unable to figure out how to implement this after many attempts. Please could someone point me in the right direction. I have watched the excellent video and read through the guide.
Thanks

Code: Select all

return {
	on = {
		devices = {
			'Outside Shade',
		}
	},
execute = function(domoticz, device)
        if
            (device.name == 'Outside Shade' and device.temperature <= 12.5) then
                domoticz.devices('Evo Color').setMode(domoticz.EVOHOME_MODE_AUTO)
elseif
            (device.name == 'Outside Shade' and device.temperature >= 12.6) then
                domoticz.devices('Evo Color').setMode(domoticz.EVOHOME_MODE_HEATINGOFF)
                

                end
	end
}/code]

My
User avatar
waltervl
Posts: 5792
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Heating Script struggling to understand

Post by waltervl »

For running between 9-16 use
timer = { 'between 09:00 and 16:00' },

For running the script with your dummy switch you can just use it in the and statement like
devices('Heating Auto').active -- Means device is On

Code: Select all

return {
    on = {
            timer = { 'between 09:00 and 16:00' },
 ,           devices = { 'Outside Shade' , 'Heating Auto'}
    },
execute = function(domoticz, device)
        if (domoticz.devices('Outside Shade').temperature < 12.6 and domoticz.devices('Heating Auto').active ) then
                domoticz.devices('Evo Color').setMode(domoticz.EVOHOME_MODE_AUTO)
         elseif  (domoticz.devices('Outside Shade').temperature >= 12.6 and domoticz.devices('Heating Auto').active ) then
                domoticz.devices('Evo Color').setMode(domoticz.EVOHOME_MODE_HEATINGOFF)         
             end
	end
}
Script is not tested, could contain type errors.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
digital
Posts: 12
Joined: Tuesday 12 December 2017 16:16
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Contact:

Re: Heating Script struggling to understand

Post by digital »

Thank you very much for your help, very much appreciated.
I have tried the script and the following error is showing in the log.......

2021-11-19 15:08:34.740 Error: EventSystem: Lua script Heating Test V1 did not return a commandArray

There is no error showing in the Domoticz inbuilt editor and I am unable to see the cause of the error.

Thanks
User avatar
waltervl
Posts: 5792
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Heating Script struggling to understand

Post by waltervl »

Did you accidentally create a LUA script instead of a dzVents script?
I think there is a comma misplaced on the 4th line

, devices = { 'Outside Shade' , 'Heating Auto'}
should be
devices = { 'Outside Shade' , 'Heating Auto'}
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
digital
Posts: 12
Joined: Tuesday 12 December 2017 16:16
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Contact:

Re: Heating Script struggling to understand

Post by digital »

Thank you so much.
Yes my mistake. I had created the script in LUA.
Script now running with no error (I did see the comma as the inbuilt editor flagged it sorry forgot to say).
Will test out and report back on how things go.
This is the first time I have had success with dzVents with your excellent help. I am very grateful for your time.
Thank you
User avatar
waltervl
Posts: 5792
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Heating Script struggling to understand

Post by waltervl »

No problem, have fun!
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
digital
Posts: 12
Joined: Tuesday 12 December 2017 16:16
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Contact:

Re: Heating Script struggling to understand

Post by digital »

Having tested out for a few days things are not working quite how I thought they would.
It’s probably myself not explaining things or something I am downing incorrectly.
The script works fine with”Heating Auto” switched off but as soon as I turn the switch on the script turns heating from off to normal regardless of what the time of day is.
My aim is for the script to run between 09:00 - 16:00 only when “Heating Auto” is switched On.
Thanks

Code: Select all

 return {
    on = {
            timer = { 'between 09:00 and 16:00' },
            devices = { 'Outside Shade' , 'Heating Auto'}
    },
execute = function(domoticz, device)
        if (domoticz.devices('Outside Shade').temperature < 12.1 and domoticz.devices('Heating Auto').active ) then
                domoticz.devices('Evo Color').setMode(domoticz.EVOHOME_MODE_AUTO)
         elseif  (domoticz.devices('Outside Shade').temperature >= 12.1 and domoticz.devices('Heating Auto').active ) then
                domoticz.devices('Evo Color').setMode(domoticz.EVOHOME_MODE_HEATINGOFF)         
             end
	end
}
EddyG
Posts: 1042
Joined: Monday 02 November 2015 5:54
Target OS: -
Domoticz version:

Re: Heating Script struggling to understand

Post by EddyG »

It should be something like this.

Code: Select all

 return {
    on = {
            timer = { 'every minute between 09:00 and 16:00' }
    },
execute = function(domoticz, device)
	if domoticz.devices('Heating Auto').state == 'On' then
	        if (domoticz.devices('Outside Shade').temperature < 12.1 and domoticz.devices('Heating Auto').active ) then
        	        domoticz.devices('Evo Color').setMode(domoticz.EVOHOME_MODE_AUTO)
	         elseif  (domoticz.devices('Outside Shade').temperature >= 12.1 and domoticz.devices('Heating Auto').active ) then
        	        domoticz.devices('Evo Color').setMode(domoticz.EVOHOME_MODE_HEATINGOFF)         
             	end
            end
        else
        	<do something here like switching off>
	end
}
You could delete the

Code: Select all

 and domoticz.devices('Heating Auto').active
part. I don't see the use for it.
Also not tested...
digital
Posts: 12
Joined: Tuesday 12 December 2017 16:16
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Contact:

Re: Heating Script struggling to understand

Post by digital »

The “Heating Auto” switch is for overriding the script when we are not at home.
Thanks
rrozema
Posts: 470
Joined: Thursday 26 October 2017 13:37
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Delft
Contact:

Re: Heating Script struggling to understand

Post by rrozema »

EddyG wrote: Monday 22 November 2021 13:06 It should be something like this.

Code: Select all

 return {
    on = {
            timer = { 'every minute between 09:00 and 16:00' }
    },
execute = function(domoticz, device)
	if domoticz.devices('Heating Auto').state == 'On' then
	        if (domoticz.devices('Outside Shade').temperature < 12.1 and domoticz.devices('Heating Auto').active ) then
        	        domoticz.devices('Evo Color').setMode(domoticz.EVOHOME_MODE_AUTO)
	         elseif  (domoticz.devices('Outside Shade').temperature >= 12.1 and domoticz.devices('Heating Auto').active ) then
        	        domoticz.devices('Evo Color').setMode(domoticz.EVOHOME_MODE_HEATINGOFF)         
             	end
            end
        else
        	<do something here like switching off>
	end
}
You could delete the

Code: Select all

 and domoticz.devices('Heating Auto').active
part. I don't see the use for it.
Also not tested...
The .active attribute is an attribute available for all device types that is true when the device's state is "On", "Open", or whatever the 'active' state is for that specific device type. I.e. checking for .active has the same effect as checking for .state == "On" but it avoids all the possible errors like .state == "on" or .State == "On" or or .state == "Open", etc. This is why it is recommended to use .active instead .state == "On". Device attributes and methods for all devices
EddyG
Posts: 1042
Joined: Monday 02 November 2015 5:54
Target OS: -
Domoticz version:

Re: Heating Script struggling to understand

Post by EddyG »

Of course you are right. But when I start scripting dzVents long time ago, I used the .state == 'On' and that worked fine without syntax errors, and it is handy when you have selectors switches. I meant that the 2 times .active statements could be replace by just 1 statement, and the triggers on the 2 devices as @waltervl scripted are not used.
User avatar
waltervl
Posts: 5792
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Heating Script struggling to understand

Post by waltervl »

I agree, "my" script could be better, but "my" script should work.
If Heating Auto is On it will switch EVO Mode based on temperature.
If this is not working add some logging lines to check if the temperature is read correctly and as expected.

Code: Select all

domoticz.log('Temp of Ouside Shade =' .. domoticz.devices('Outside Shade').temperature, domoticz.LOG_INFO)
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
rrozema
Posts: 470
Joined: Thursday 26 October 2017 13:37
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Delft
Contact:

Re: Heating Script struggling to understand

Post by rrozema »

And for OP digital: each dzvents script consists of a few mandatory sections:
  • execute section : WHAT needs to be executed
  • on section : WHEN to execute it.
The on section can contain multiple entries for when Domoticz must call the execute function. If you specify more than one entry, Domoticz will call the execute function when the condition(s) is/are met for each of those entries. So if you specify some device in the on section, the execute function will be called every time the state or value in that device changes. When you specify a timer, the execute function will be called when the timer condition is met. In your example this means the execute function will be called at every minute from 9:00 until 16:00.

So when you specified

Code: Select all

    on = {
            timer = { 'between 09:00 and 16:00' },
            devices = { 'Outside Shade' , 'Heating Auto'}
    }
this meant the execute function was called at 9:00, at 9:01, at 9:02, .... and at 16:00. But also when the status of device 'Outside Shade' changed or the status for device 'Heating Auto' changed. Regardless of the time when those changes were done: i.e. also outside of the 9:00 to 16:00 time window.

You have to be very careful if you combine timer and device on-clauses and even if you combine multiple on clauses of the same type. It is very easy to make mistakes if you have more than 1 reason why the execute function can be called. The best advice I can give you for any dzvents script's execute function that has more than 1 item in it's on-section is to always first check the reason why the execute function was called: First check for the type of thr "trigger". You can determine this information from the 2nd parameter tot the execute() function as described here. This 2nd parameter will be a device-object if the reason to call your execute() function was a device in your on list was changed. But it will be a timer-object if the execute was called because the current time equals one of your timer clauses in the on-section. And so on for all of the possible on types, as listed in the documentation. So the first thing you should do in each execute() function when you have multiple types is check for the type of the object in that 2nd parameter. Then if you need to, you should check which device, timer, etc specifically it was that caused execute() to get called. This you can do by looking at the attributes of the object in that 2nd parameter. For a device object that can be for example the .name attribute or the .idx attribute. For a timer object that can be the .trigger attribute.

Code: Select all

execute = function(domoticz, item)
   if item.isDevice then
      -- todo: do something when a device changes.
      -- To check for one specific device, you can check for the device name or idx. For example:
      if item.name == "My 1st device" then
         -- todo: do something that only needs done when My 1st device changes.
      elseif item.idx == 204 then
         -- todo: do something when device with idx 204 changes.
      else
         -- we were called for a device that I didn't expect. You can ignore or raise an error here.
      end
   elseif item.isTimer then
      -- todo: do something when a time event occurs.
      -- To check for one specific timer, you can again check for the .name attribute. This will match 
      -- the text that you specified in the on-section. For example:
      if item.trigger == "every minute" then
          -- todo: do something that needs to be done every minute.
          -- Please note that if you specify a time range the execute function will be called on every 
          -- minute that falls within the specified time range. So if you specify for example 
          -- "between 9:00 and 9:05", the execute function will be called at 9:00, 9:01, 9:02, 9:03,
          -- 9:04 and 9:05. There are many more ways of specifying a range, for example "at daytime" 
          -- triggers the execute function every minute from sunrise to sunset.
      elseif item.trigger == "every hour" then
         -- todo: do something that needs to be done every hour. Please note that at for example 9:00 
         -- the execute() function will be called twice: once with a timer object in the 2nd parameter 
         -- with .name = "every minute" and another time with a timer object with .name = "every hour".
      else
         -- Some other timer definition that we didn't expect caused execute() to get called. You can 
         -- coose to ignore it or raise an error or you can decide that you still want to do something 
         -- no matter what. It's up to you and what you're trying to achieve.
      end
   else
      -- Some type of trigger we didn't expect made us get called. You can ignore it or raise an error here.
   end
end
I personally don't very much like to use timer triggers in dzvents scripts because they cause the execute
function to be called so often and you'll need all sorts of logic to make sure that you don't do something
that has already been done a minute before. Instead of that I like to define some dummy switch device,
then write the script to execute what needs to be done when the switch goes off or on. Then in the UI i
can easily have the switch go to some state at specific times via the 'timers' button on the switch. This
makes coding scripts a lot easier.
digital
Posts: 12
Joined: Tuesday 12 December 2017 16:16
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Contact:

Re: Heating Script struggling to understand

Post by digital »

First of all Thank you everyone for all the help and advice.
I have tried the script that waltervl very kindly provided and which is very much appreciated in helping me out. The scripts not quite doing what I set out to achieve. So I was with my very limited knowledge and reading through the wiki several times trying things out which is when I changed from .active to .state .
I will take on board all the comments and help and have another try.
Many thanks to all.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest