Wake-up alarm: Compare .text value (string)  [Solved]

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

Moderator: leecollings

Post Reply
dizzeenl
Posts: 31
Joined: Wednesday 14 November 2018 23:57
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Dordrecht
Contact:

Wake-up alarm: Compare .text value (string)

Post by dizzeenl »

I have made a Auto wake-up alarm script to wake me up in the morning (light, radio and rollershutter).

With a custom switch, the Wakeup selector, i can select: off, auto/calender of multiple time options

The auto/calender options loads a text string from a custom switch (text) where the next calender item is linked into (ical).
The time options (and the second part of the auto/calender option) are using the script below.

Code: Select all

return {
	active = true,
	on = {
		devices = {'Wakeup selector'},
		timer = {'at 04:00'}
	},
	execute = function(domoticz)
		if domoticz.devices('Wakeup selector').levelName == 'Uitgeschakeld' then
		    domoticz.variables('WEKKER').set('00:00')
		    
		elseif domoticz.devices('Wakeup selector').levelName == 'Agenda' then
	        
	        if 	domoticz.devices('Agenda').text == 'D8G' then
	            domoticz.variables('WEKKER').set('05:45')
	        end
	        if 	domoticz.devices('Agenda').text == 'IEG' then
	            domoticz.variables('WEKKER').set('06:45')
	        end
	        if 	domoticz.devices('Agenda').text == '8IG' then
	            domoticz.variables('WEKKER').set('07:30')
	        end
	     else
    	    if 	domoticz.devices('Wakeup selector').levelName == '06:17' then
	            domoticz.variables('WEKKER').set('05:45')
	        end
	        if 	domoticz.devices('Wakeup selector').levelName == '06:37' then
	            domoticz.variables('WEKKER').set('06:05')
	        end
.... and so on....
      end
	end
}
As you can see it's not the most perfect way to get the job done.

1. i have troubles to compare the .text (string) form my calendar with the given codes. Somehow it never result in a match. if i print the value in my log it shows identical values with the one to compare with but it refuses to work :cry: .

2. Is there a cleaner option for handling the custom time selections? Because i cannot save the selector.levelName as a time variable i cannot count and say like, "set wekker .levelname minus 30 minutes." (not a big problem, just wondering how to solve this)

any tips/advice to solve this problem?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Wake-up alarm: Compare .text value (string)

Post by waaren »

dizzeenl wrote: Monday 04 May 2020 23:41 I have made a Auto wake-up alarm script to wake me up in the morning (light, radio and rollershutter).

1. i have troubles to compare the .text (string) form my calendar with the given codes. Somehow it never result in a match. if i print the value in my log it shows identical values with the one to compare with but it refuses to work :cry: .

2. Is there a cleaner option for handling the custom time selections? Because i cannot save the selector.levelName as a time variable i cannot count and say like, "set wekker .levelname minus 30 minutes." (not a big problem, just wondering how to solve this)

any tips/advice to solve this problem?
Can you try with this? I added some extra code to enable you to see what happens That code can be deactivated when the script is ready.
Please feel free to ask for clarification if things are not clear.

Code: Select all

return 
{
    active = true,

    on = 
    {
        devices = 
        {
            'Wakeup selector',
        },
        timer = 
        {
            'at 04:00',
        },
    },
    
    logging =
    {
        level = domoticz.LOG_DEBUG,  -- change to domoticz.LOG_ERROR when testing / development is ready 
        marker = 'Wekker',
    },
    
    
    execute = function(domoticz)
        local dz = domoticz -- too lazy to type domoticz 
        local lodash = dz.utils._
        
        local agenda = dz.devices('Agenda')   -- virtual text device
        local selector = dz.devices('Wakeup selector') -- selector
        local wekker = dz.variables('WEKKER') -- time variable
        
        deltaTime = 32
        
        local function printAttributes(object) -- debug function
            for key, value in pairs(object) do
                if type(value) == 'table' then
                    dz.log(object.name .. ' >> ' .. lodash.str(key) .. ' : (table)',dz.LOG_DEBUG)
                elseif type(value) ~= 'function' then
                    dz.log(object.name .. ' >> ' .. tostring(key) .. ' : ' .. tostring(value),dz.LOG_DEBUG)
                end 
            end 
        end
        
        printAttributes(agenda) -- only during development / test 
        printAttributes(wekker) -- only during development / test 
        
        function makeMinutes(myTime)  -- produce minutes from hh:mm string
            local pattern = "(%d+)%:(%d+)"
            local hours, minutes = myTime:match(pattern)
            return hours * 60 + minutes
        end

        function makeTimeString(myMinutes) -- produce hh:mm from minutes
            if myMinutes < 0 then 
            	myMinutes = myMinutes + 1440 
            else
                myMinutes = myMinutes % 1440 
            end
            
            local hours = dz.utils.leadingZeros(math.floor(myMinutes / 60),2)
            local minutes = dz.utils.leadingZeros(math.floor(myMinutes % 60),2)
            return hours .. ':' .. minutes
        end

        local labels = 
        { 
            ['D8G'] = '05:45',
            ['IEG'] = '06:45',
            ['8IG'] = '07:30',
        }

        if selector.levelName == 'Uitgeschakeld' then
            wekker.set('00:00')
        elseif selector.levelName == 'Agenda' then
            wekker.set(labels[agenda.text])
        else
            wekker.set(makeTimeString(makeMinutes(selector.levelName) - deltaTime))
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
dizzeenl
Posts: 31
Joined: Wednesday 14 November 2018 23:57
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Dordrecht
Contact:

Re: Wake-up alarm: Compare .text value (string)

Post by dizzeenl »

Thanks for the fast reply. I understand in general how your solution works. i have not enough experience with tables, lodash and other codes you used but it's nice to be able to read how it works :D. The calulation with the delta time works like a charm. Many thanks 8-)

Converting the agenda string into a label gif still a nil as result.
"2020-05-05 12:26:21.584 Error: Error updating variable WEKKER: Not a valid time notation (HH:MM)"

Code: Select all

2020-05-05 12:26:21.246 Status: User: Admin initiated a switch command (910/Wakeup selector/Set Level)
2020-05-05 12:26:21.355 Status: dzVents: Debug: Dumping domoticz data to /home/pi/Domoticz/scripts/dzVents/domoticzData.lua
2020-05-05 12:26:21.480 Status: dzVents: Debug: Processing device-adapter for Wakeup selector: Switch device adapter
2020-05-05 12:26:21.481 Status: dzVents: Debug: dzVents version: 3.0.4
2020-05-05 12:26:21.481 Status: dzVents: Debug: Event triggers:
2020-05-05 12:26:21.481 Status: dzVents: Debug: - Device: Wakeup selector
2020-05-05 12:26:21.556 Status: dzVents: Info: Handling events for: "Wakeup selector", value: "Agenda"
2020-05-05 12:26:21.557 Status: dzVents: Info: Wekker: ------ Start internal script: 02 WEKKER_HANDLER 2: Device: "Wakeup selector (VIRTUAL)", Index: 910
2020-05-05 12:26:21.560 Status: dzVents: Debug: Wekker: Processing device-adapter for Agenda: Text device
2020-05-05 12:26:21.561 Status: dzVents: Debug: Wekker: Agenda >> isDevice : true
2020-05-05 12:26:21.561 Status: dzVents: Debug: Wekker: Agenda >> baseType : device
2020-05-05 12:26:21.562 Status: dzVents: Debug: Wekker: Agenda >> timedOut : true
2020-05-05 12:26:21.562 Status: dzVents: Debug: Wekker: Agenda >> deviceType : General
2020-05-05 12:26:21.562 Status: dzVents: Debug: Wekker: Agenda >> hardwareTypeValue : 15
2020-05-05 12:26:21.562 Status: dzVents: Debug: Wekker: Agenda >> hardwareID : 7
2020-05-05 12:26:21.562 Status: dzVents: Debug: Wekker: Agenda >> rawData : (table)
2020-05-05 12:26:21.562 Status: dzVents: Debug: Wekker: Agenda >> isGroup : false
2020-05-05 12:26:21.562 Status: dzVents: Debug: Wekker: Agenda >> idx : 911
2020-05-05 12:26:21.562 Status: dzVents: Debug: Wekker: Agenda >> signalLevel : 12
2020-05-05 12:26:21.562 Status: dzVents: Debug: Wekker: Agenda >> switchTypeValue : 0
2020-05-05 12:26:21.562 Status: dzVents: Debug: Wekker: Agenda >> text : L8G
2020-05-05 12:26:21.563 Status: dzVents: Debug: Wekker: Agenda >> deviceSubType : Text
2020-05-05 12:26:21.563 Status: dzVents: Debug: Wekker: Agenda >> bState : false
2020-05-05 12:26:21.563 Status: dzVents: Debug: Wekker: Agenda >> id : 911
2020-05-05 12:26:21.563 Status: dzVents: Debug: Wekker: Agenda >> hardwareName : VIRTUAL
2020-05-05 12:26:21.563 Status: dzVents: Debug: Wekker: Agenda >> lastUpdate : (table)
2020-05-05 12:26:21.563 Status: dzVents: Debug: Wekker: Agenda >> _adapters : (table)
2020-05-05 12:26:21.563 Status: dzVents: Debug: Wekker: Agenda >> isTimer : false
2020-05-05 12:26:21.563 Status: dzVents: Debug: Wekker: Agenda >> name : Agenda
2020-05-05 12:26:21.563 Status: dzVents: Debug: Wekker: Agenda >> icon : text
2020-05-05 12:26:21.563 Status: dzVents: Debug: Wekker: Agenda >> isSecurity : false
2020-05-05 12:26:21.563 Status: dzVents: Debug: Wekker: Agenda >> _data : (table)
2020-05-05 12:26:21.564 Status: dzVents: Debug: Wekker: Agenda >> active : false
2020-05-05 12:26:21.564 Status: dzVents: Debug: Wekker: Agenda >> isHTTPResponse : false
2020-05-05 12:26:21.564 Status: dzVents: Debug: Wekker: Agenda >> hardwareId : 7
2020-05-05 12:26:21.564 Status: dzVents: Debug: Wekker: Agenda >> sValue : L8G
2020-05-05 12:26:21.564 Status: dzVents: Debug: Wekker: Agenda >> nValue : 0
2020-05-05 12:26:21.564 Status: dzVents: Debug: Wekker: Agenda >> _state : L8G
2020-05-05 12:26:21.564 Status: dzVents: Debug: Wekker: Agenda >> isScene : false
2020-05-05 12:26:21.564 Status: dzVents: Debug: Wekker: Agenda >> protected : false
2020-05-05 12:26:21.564 Status: dzVents: Debug: Wekker: Agenda >> unit : 1
2020-05-05 12:26:21.564 Status: dzVents: Debug: Wekker: Agenda >> deviceId : 00082911
2020-05-05 12:26:21.565 Status: dzVents: Debug: Wekker: Agenda >> changed : false
2020-05-05 12:26:21.565 Status: dzVents: Debug: Wekker: Agenda >> switchType : On/Off
2020-05-05 12:26:21.565 Status: dzVents: Debug: Wekker: Agenda >> description :
2020-05-05 12:26:21.565 Status: dzVents: Debug: Wekker: Agenda >> _nValue : 0
2020-05-05 12:26:21.565 Status: dzVents: Debug: Wekker: Agenda >> hardwareType : Dummy (Does nothing, use for virtual switches only)
2020-05-05 12:26:21.565 Status: dzVents: Debug: Wekker: Agenda >> isVariable : false
2020-05-05 12:26:21.565 Status: dzVents: Debug: Wekker: Agenda >> state : L8G
2020-05-05 12:26:21.565 Status: dzVents: Debug: Wekker: WEKKER >> isDevice : false
2020-05-05 12:26:21.565 Status: dzVents: Debug: Wekker: WEKKER >> isSystem : false
2020-05-05 12:26:21.565 Status: dzVents: Debug: Wekker: WEKKER >> baseType : variable
2020-05-05 12:26:21.565 Status: dzVents: Debug: Wekker: WEKKER >> isCustomEvent : false
2020-05-05 12:26:21.565 Status: dzVents: Debug: Wekker: WEKKER >> type : time
2020-05-05 12:26:21.565 Status: dzVents: Debug: Wekker: WEKKER >> value : 06:18
2020-05-05 12:26:21.566 Status: dzVents: Debug: Wekker: WEKKER >> isScene : false
2020-05-05 12:26:21.566 Status: dzVents: Debug: Wekker: WEKKER >> changed : false
2020-05-05 12:26:21.566 Status: dzVents: Debug: Wekker: WEKKER >> isHTTPResponse : false
2020-05-05 12:26:21.566 Status: dzVents: Debug: Wekker: WEKKER >> lastUpdate : (table)
2020-05-05 12:26:21.566 Status: dzVents: Debug: Wekker: WEKKER >> isHardware : false
2020-05-05 12:26:21.566 Status: dzVents: Debug: Wekker: WEKKER >> time : (table)
2020-05-05 12:26:21.566 Status: dzVents: Debug: Wekker: WEKKER >> isTimer : false
2020-05-05 12:26:21.566 Status: dzVents: Debug: Wekker: WEKKER >> isSecurity : false
2020-05-05 12:26:21.566 Status: dzVents: Debug: Wekker: WEKKER >> trigger : WEKKER
2020-05-05 12:26:21.566 Status: dzVents: Debug: Wekker: WEKKER >> name : WEKKER
2020-05-05 12:26:21.566 Status: dzVents: Debug: Wekker: WEKKER >> isGroup : false
2020-05-05 12:26:21.566 Status: dzVents: Debug: Wekker: WEKKER >> id : 8
2020-05-05 12:26:21.566 Status: dzVents: Debug: Wekker: WEKKER >> isVariable : true
2020-05-05 12:26:21.567 Status: dzVents: Info: Wekker: ------ Finished 02 WEKKER_HANDLER 2
2020-05-05 12:26:21.567 Status: dzVents: Debug: Commands sent to Domoticz:
2020-05-05 12:26:21.567 Status: dzVents: Debug: - Variable = {["_trigger"]=true, ["value"]="", ["idx"]=8}
2020-05-05 12:26:21.567 Status: dzVents: Debug: =====================================================
2020-05-05 12:26:21.568 Status: EventSystem: Script event triggered: /home/pi/Domoticz/dzVents/runtime/dzVents.lua
(i added L8G as a label)

If i print dz.log("2 "..labels[(agenda.text)],dz.LOG_DEBUG) i got nothing
if i print dz.log("1 "..agenda.text,dz.LOG_DEBUG) i get 1 L8G
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Wake-up alarm: Compare .text value (string)  [Solved]

Post by waaren »

dizzeenl wrote: Tuesday 05 May 2020 12:49 Converting the agenda string into a label gif still a nil as result.
"2020-05-05 12:26:21.584 Error: Error updating variable WEKKER: Not a valid time notation (HH:MM)"
Only thing I can think of is a space, tab or similar in the text.
Below (updated) script will recognize these and filter them out.

Code: Select all

return 
{
    active = true,

    on = 
    {
        devices = 
        {
            'Wakeup selector',
        },
        timer = 
        {
            'at 04:00',
        },
    },
    
    logging =
    {
        level = domoticz.LOG_DEBUG,  -- change to domoticz.LOG_ERROR when testing / development is ready 
        marker = 'Wekker',
    },
    
    
    execute = function(domoticz)
        local dz = domoticz -- too lazy to type domoticz 
        local lodash = dz.utils._
        
        local agenda = dz.devices('Agenda')   -- virtual text device
        local selector = dz.devices('Wakeup selector') -- selector
        local wekker = dz.variables('WEKKER') -- time variable
        
        local deltaTime = 32
        
        local function printAttributes(object) -- debug function
            for key, value in pairs(object) do
                if type(value) == 'table' then
                    dz.log(object.name .. ' >> ' .. lodash.str(key) .. ' : (table)',dz.LOG_DEBUG)
                elseif type(value) ~= 'function' then
                    dz.log(object.name .. ' >> ' .. tostring(key) .. ' : ' .. tostring(value),dz.LOG_DEBUG)
                end 
            end 
        end
        
        printAttributes(agenda) -- only during development / test 
        printAttributes(wekker) -- only during development / test 
        
        local function makeMinutes(myTime)  -- produce minutes from hh:mm string
            local pattern = "(%d+)%:(%d+)"
            local hours, minutes = myTime:match(pattern)
            return hours * 60 + minutes
        end

        local function makeTimeString(myMinutes) -- produce hh:mm from minutes
            if myMinutes < 0 then 
                myMinutes = myMinutes + 1440 
            else
                myMinutes = myMinutes % 1440 
            end
            
            local hours = dz.utils.leadingZeros(math.floor(myMinutes / 60),2)
            local minutes = dz.utils.leadingZeros(math.floor(myMinutes % 60),2)
            return hours .. ':' .. minutes
        end
        
        local function sanitizeString(rawString)
            local sanitizedString = rawString
            if rawString:find('%s') then
                dz.log(rawString .. ' contains blank character(s) ', dz.LOG_DEBUG )
                sanitizedString = rawString:gsub('%s','')
            end
            return sanitizedString
        end
        
        -- agenda.text = 'D8G\t\r\n' -- test string
        local labels = 
        { 
            ['D8G'] = '05:45',
            ['IEG'] = '06:45',
            ['8IG'] = '07:30',
        }

        if selector.levelName == 'Uitgeschakeld' then
            wekker.set('00:00')
        elseif selector.levelName == 'Agenda' then
            wekker.set(labels[sanitizeString(agenda.text)])
        else
            wekker.set(makeTimeString(makeMinutes(selector.levelName) - deltaTime))
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest