Decimal notation

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

Moderator: leecollings

Post Reply
User avatar
jacobsentertainment
Posts: 223
Joined: Thursday 01 October 2020 1:47
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021-1
Location: Not @ home
Contact:

Decimal notation

Post by jacobsentertainment »

Hi all,

I got this little script made up with some copy paste, it works with three small flaws.
The notification isn't according to the timer 'every 1 minutes' but constantly.
The temperature how can I change the decimals? a single digit is more then enough.
And how can I only send a message to telegram, currently it sends to mail and telegram.

In the log I get a temperature with only two decimals but in the message that's send to telegram/mail it shows 12 decimals!

Code: Select all

return {
	on = {
	    timer = {
			'every 1 minutes'
		},
		devices = { 43, 65, 66, 42, 194
			},
		},

	logging = {
        level = domoticz.LOG_INFO,
        marker = "Sauna"
    },

	execute = function(domoticz)

	    local Home = domoticz.devices(199)
	    local Bedtime = domoticz.devices(66)
	    local Sauna = domoticz.devices(43)
	    local Sauna_temp = domoticz.devices(42).temperature


        if Sauna.state == 'On'  and Home.state == 'On' then
            Sauna.switchOff().afterSec(180)
	end
	
	   if Sauna.state == 'On' then
	        domoticz.notify("Sauna","Temperatuur is nu "..Sauna_temp.." !",domoticz.PRIORITY_HIGH, "telegram")
    end
end
}
User avatar
waltervl
Posts: 6691
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2025.1
Location: NL
Contact:

Re: Decimal notation

Post by waltervl »

You have two triggers, every minute and every change in the mentioned devices.

Try 'every minute' instead of 'every 1 minutes'.
What happens if you use 'every 2 minutes' ?

Also remove the devices from the on = devices { } section that you do not need to trigger the script. You only need the Home and Sauna devices.
Every other device in that list that changes will trigger the script
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
User avatar
jacobsentertainment
Posts: 223
Joined: Thursday 01 October 2020 1:47
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021-1
Location: Not @ home
Contact:

Re: Decimal notation

Post by jacobsentertainment »

waltervl wrote: Monday 11 October 2021 18:36 What happens if you use 'every 2 minutes' ?
Also remove the devices
Done, cleaned up and changed time trigger.

Code: Select all

return {
        on = {
          devices = {
            199, 43
        },
		timer = {
			'every 2 minutes'
		},
    },
    
    	logging = {
        level = domoticz.LOG_INFO,
        marker = "Sauna"
    },
        execute = function(domoticz, devices)
            local Home = domoticz.devices(199)
            local Sauna = domoticz.devices(43)
            local Temp= domoticz.devices(42).temperature
            
            if (Sauna.state == 'On') then and Home.state == 'On' then
                Sauna.switchOff().afterSec(180)
        end 
                
	   if Sauna.state == 'On' then
	        domoticz.notify("Sauna","Temperatuur is nu "..Temp.." !",domoticz.PRIORITY_HIGH, "telegram")
    end
end
}
The interval between messages is now as per timer, except the device doesn't switch off :o
riko
Posts: 90
Joined: Saturday 22 August 2020 13:36
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Decimal notation

Post by riko »

You might try to remove the first 'then' in this line:

Code: Select all

if (Sauna.state == 'On') then and Home.state == 'On' then
                Sauna.switchOff().afterSec(180)
User avatar
jacobsentertainment
Posts: 223
Joined: Thursday 01 October 2020 1:47
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021-1
Location: Not @ home
Contact:

Re: Decimal notation

Post by jacobsentertainment »

riko wrote: Monday 11 October 2021 19:46 You might try to remove the first 'then' in this line:

Code: Select all

if (Sauna.state == 'On') then and Home.state == 'On' then
                Sauna.switchOff().afterSec(180)
Did that, the editor already mentioned it prior to save it. :oops:
EddyG
Posts: 1042
Joined: Monday 02 November 2015 5:54
Target OS: -
Domoticz version:

Re: Decimal notation

Post by EddyG »

Is your 12 decimal problem solved? If not change.

Code: Select all

local Temp = tostring(domoticz.utils.round(domoticz.devices(42).temperature, 1))
In general is is good practice and readability to use devicenames between quotes instead of idx
User avatar
jacobsentertainment
Posts: 223
Joined: Thursday 01 October 2020 1:47
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021-1
Location: Not @ home
Contact:

Re: Decimal notation

Post by jacobsentertainment »

EddyG wrote: Monday 11 October 2021 20:39 Is your 12 decimal problem solved? If not change.

Code: Select all

local Temp = tostring(domoticz.utils.round(domoticz.devices(42).temperature, 1))
Thanks for that one, I would of never found that.
EddyG wrote: Monday 11 October 2021 20:39 In general is is good practice and readability to use devicenames between quotes instead of idx
I rather use the idx instead of the name, sometimes i change names or hide them by using the $ then I don't have to go trough the scripts again to update names. In some scripts I have them as a comment behind the local name.
User avatar
waltervl
Posts: 6691
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2025.1
Location: NL
Contact:

Re: Decimal notation

Post by waltervl »

Good work.
I also like to use IDX value instead of names, for readability you can add a comment behind.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
User avatar
jacobsentertainment
Posts: 223
Joined: Thursday 01 October 2020 1:47
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021-1
Location: Not @ home
Contact:

Re: Decimal notation

Post by jacobsentertainment »

So far so good, all seems to work now :D
Spoiler: show
return {
on = {
devices = {
199, 43
},
timer = {
'every 2 minutes'
},
},

logging = {
level = domoticz.LOG_INFO,
marker = "Sauna"
},
execute = function(domoticz, devices)
local Home = domoticz.devices(199)
local Sauna = domoticz.devices(43)
local Temp = tostring(domoticz.utils.round(domoticz.devices(42).temperature, 1))

if Sauna.state ~= 'On' and Home.state == 'On' then
Sauna.switchOff().afterSec(900)
end

if Sauna.state == 'On' then
domoticz.notify("Sauna","Temperatuur is nu "..Temp.."°",domoticz.PRIORITY_HIGH, "telegram")
end
end
}
Only thing still need to figure out is how to only message to telegram, now the message is send to both..
sauna.jpg
sauna.jpg (51.43 KiB) Viewed 1227 times
rrozema
Posts: 470
Joined: Thursday 26 October 2017 13:37
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Delft
Contact:

Re: Decimal notation

Post by rrozema »

The execute() function gets executed for every trigger you defined in the on = {} - section. I.e. yours executes once every two minutes, but also when one of the devices 199 or 43 changes state. To distinguish between the different reasons for executing, you need the 2nd parameter to the execute() function. I don't know what exactly it is you want to achieve with your script, so here's a template you can use to build what you want. I also added device 42 (your temperature sensor) to show that you can use this to trigger the notification whenever the temperature changes.

Code: Select all

return {
        on = {
		devices = {
			199, 43, 42
		},
		timer = {
			'every 2 minutes'
		}
	},
    	logging = {
	        level = domoticz.LOG_INFO,
        	marker = "Sauna"
	},
        execute = function(domoticz, item)
		local Home = domoticz.devices(199)
		local Sauna = domoticz.devices(43)
		local TempDevice = domoticz.devices(42)
            
		if nil == Home then
	        	domoticz.log( 'Device 199 not found.', domoticz.LOG_ERROR)
		elseif nil == Sauna then
	        	domoticz.log( 'Device 43 not found.', domoticz.LOG_ERROR)
		elseif nil == TempDevice then
	        	domoticz.log( 'Device 42 not found.', domoticz.LOG_ERROR)
		else
			if item.isTimer then
		        	domoticz.log( 'execute activated by timer ' .. item.name .. '.', domoticz.LOG_INFO)
				-- to do: add code here to do whatever you want to do every 2 minutes.
			elseif item.isDevice then
				if item.idx == 199 then
		        		domoticz.log( 'Device ' .. tostring(item.idx) .. ', ' .. item.name .. ' changed.', domoticz.LOG_INFO)
					-- to do: add code here you want executed whenever device 199 changes state.
					if item.active then
			        		domoticz.log( 'Device ' .. tostring(item.idx) .. ', ' .. item.name .. ' switched on.', domoticz.LOG_INFO)
						-- to do: add code here you want executed when device 199 switches on.
					else
			        		domoticz.log( 'Device ' .. tostring(item.idx) .. ', ' .. item.name .. ' switched off.', domoticz.LOG_INFO)
						-- to do: add code here you want execiuted when device 199 switches off.
					end
				elseif item.idx == 43 then
		        		domoticz.log( 'Device ' .. tostring(item.idx) .. ', ' .. item.name .. ' changed.', domoticz.LOG_INFO)
	            			-- to do: add code here you want executed when device 43 changes state.
	            			if item.active then
			        		domoticz.log( 'Device ' .. tostring(item.idx) .. ', ' .. item.name .. ' switched on.', domoticz.LOG_INFO)
	            				-- to do: add code her you want executed when device 43 switches on.
			        		domoticz.notify( "Sauna", "Temperatuur is nu ".. tostring(math.floor(TempDevice.temperature+0.5)) .. " !", domoticz.PRIORITY_HIGH, "telegram")
	            			else
			        		domoticz.log( 'Device ' .. tostring(item.idx) .. ', ' .. item.name .. ' switched off.', domoticz.LOG_INFO)
	            				-- to do: add code her you want executed when device 43 switches off.
	            			end
				elseif item.idx == 42 then
		        		domoticz.log( 'Device ' .. tostring(item.idx) .. ', ' .. item.name .. ' changed.', domoticz.LOG_INFO)
		        		if Sauna.active then
			        		domoticz.notify( "Sauna", "Temperatuur is nu ".. tostring(math.floor(item.temperature+0.5)) .. " !", domoticz.PRIORITY_HIGH, "telegram")
			        	end
	            		else
	            	        	domoticz.log( 'Unknown device '.. tostring(device.idx) .. ' triggered the script.', domoticz.LOG_ERROR)
	            	        end
			end
		end
            
--            if (Sauna.state == 'On') then and Home.state == 'On' then
--                Sauna.switchOff().afterSec(180)
--        end 
                
--	   if Sauna.state == 'On' then
--	        domoticz.notify("Sauna","Temperatuur is nu "..Temp.." !",domoticz.PRIORITY_HIGH, "telegram")
--    end
end
}
This code has probably a lot of typos because I didn't test it all. But I hope it does give you an idea of what you can do in the execute() function.
User avatar
jacobsentertainment
Posts: 223
Joined: Thursday 01 October 2020 1:47
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021-1
Location: Not @ home
Contact:

Re: Decimal notation

Post by jacobsentertainment »

rrozema wrote: Monday 11 October 2021 21:53 I don't know what exactly it is you want to achieve with your script
I want to switch on the sauna when away from home, sometimes I have a walk or bike ride and want to enjoy a nice sauna afterwards. With this script I can switch on the sauna from my phone while on my way home. Second when at home I can also switch it on and get regular updates on the current temperature waiting to enter the sauna. (the time frames are currently for testing purpose and once works like intended ill set it to my desired time)

Device 43 is the actual sauna on/off switch and device 199 is the home switch so when away from home (outside the 100km geofence) then it can't be switched on and if left on it will switch off. Also the switchOff().afterSec(7200) will switch of the sauna after two hours. Thats why I have them as triggers in there. The two minutes is only for the temperature message, that will go to 5min.

But ill sure have a good look trough you're script, thanks a lot!
rrozema
Posts: 470
Joined: Thursday 26 October 2017 13:37
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Delft
Contact:

Re: Decimal notation

Post by rrozema »

Based on your description I suggest to completely remove the timer from your script and trigger the script only on the devices changing state:

Code: Select all

-when the temperature sensor (idx=42 I think) reports a new temperature: 
    if "Sauna" is on then send a notification.
    
- when "Home" is switched off:
    if "Sauna" is on then switch "Sauna" off.

- when "Sauna" is switched on:
   if "Home" is on then 
      send a temperature notification
      start the two hours timer to switch "Sauna" off.
   else
      switch "Sauna" off    
User avatar
jacobsentertainment
Posts: 223
Joined: Thursday 01 October 2020 1:47
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021-1
Location: Not @ home
Contact:

Re: Decimal notation

Post by jacobsentertainment »

That's about correct, the last script I uploaded in the spoiler is what works now.
If I get a message each time the temperature changes then that interval would be way to much. That's why I have the timer in place to reduce the amount of messages back to every 5 minutes.
My sauna is a 9ld one with the isolation of a old shoe box, it takes about 60 minutes to reach 80° so if I get an update every 5 minutes that's good enough. The rest is reasonable simple on of with a if.
I had something not in the right order in my script, and could not figure it out on my own anymore.
I like it that in this forum is lots of feedback on how to solve it but also on helping people without skills out

Verstuurd vanaf mijn XQ-BT52 met Tapatalk

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest