Page 1 of 1

Setting starttime Switch

Posted: Wednesday 30 October 2019 20:33
by michelb
My wife has to start on differtent times in the morging on differtent days with no reoccurence. Therefore I would like te let her set the time she has to wake up. Depending on that time various devices will activate. What kind of Switch do I need? I tried a dummy switch with a preset of values, but I can't compare the actual time with the value set in the switch. The logic should eventually be something like below.

if [time set in switch <> "0:00"
If actual time >= [time set in switch]
do actions..
[time set in switch] = "0:00"
endif
endif

The only thing my wife needs to do is setting the time in the timer :)

Re: Setting starttime Switch

Posted: Wednesday 30 October 2019 22:22
by waaren
michelb wrote: Wednesday 30 October 2019 20:33 The only thing my wife needs to do is setting the time in the timer :)
For the dzVents script below you will need a selector switch. Level 0 should be named 'Off' and other levels can be times in hh:mm format.
Selectorswitch for alarmclock times
Selectorswitch for alarmclock times
alarm.png (25.8 KiB) Viewed 1213 times
When not yet familiar with dzVents please start with reading Get started Before implementing (~ 5 minutes). Special attention please for "In Domoticz go to Setup > Settings > Other and in the section EventSystem make sure the checkbox 'dzVents disabled' is not checked. Also make sure that in the Security section in the settings you allow 127.0.0.1 to not need a password. dzVents uses that port to send certain commands to Domoticz. Finally make sure you have set your current location in Setup > Settings > System > Location, otherwise there is no way to determine nighttime/daytime state."

Code: Select all

-- Alarm clock [ dzVents >= 2.4 ]

scriptVar = '20191030 Alarmclock'

return 
{
    on =
    {
        timer = { "at 00:07" },    -- daily run once to pick up target time
        httpResponses = { scriptVar }, -- Trigger the handle Json part
    },
    
    logging = { level = domoticz.LOG_DEBUG },

    execute = function(dz, item)
        local alarmTime = dz.devices('Alarm clock').levelName
        local minutesIn24Hours = 24 * 60
        
        local now = dz.time                             -- Get current time into time object
        local Time = require('Time')
        local targetTime = Time(dz.time.rawDate .. ' '  .. alarmTime .. ':00') -- Create target time object

        local function triggerAlarm(delay)
            local message = 'tring, trrring, trrring alarmclock ringing now. Was set on '.. dz.time.rawDate .. ', at ' .. dz.time.rawTime
            local url = dz.settings['Domoticz url'] .. "/json.htm?type=command&param=addlogmessage&message=" .. dz.utils.urlEncode(message)
            dz.openURL({ url = url, callback = scriptVar }).afterMin(math.floor(delay))
        end

        if not(item.isHTTPResponse) then
            dz.log('Alarm time is set at : ' .. alarmTime)
            local deltaMinutes = math.floor(now.compare(targetTime).minutes)
            if now.compare(targetTime).compare < 1 then -- targetTime is in future
                deltaMinutes = minutesIn24Hours - deltaMinutes
            end
            dz.log('Alarmactions will be triggered in ' .. deltaMinutes .. ' minutes' ,dz.LOG_DEBUG)
            triggerAlarm(deltaMinutes)
        else
            -- do wakeup actions.. here
            dz.log('wake up actions here....'  ,dz.LOG_DEBUG)
        end
    end
}

Re: Setting starttime Switch

Posted: Thursday 31 October 2019 19:27
by pvklink
Nice!
when alarm, switch a device and when the device is on, then play a wav file !

or
use dashtic that can speak when a device is on or off !

Other Options:

When
pushing the alarm switch off, kill the current alarm and dont set it again..
pushing an selector item other then 0ff set's the alarm (calls the json), then you dont need the On... and you can adjust the alarm anytime also for a short time before the on triggers...

Re: Setting starttime Switch

Posted: Saturday 31 October 2020 14:18
by Dutchsea
I got the script working and know have an alarm clock that activates a lights scene, my Sonos player at my bedroom and the heater in my bathroom.
Only get a error when the alarm is set to "Off" via the selector switch:

Code: Select all

2020-10-31 14:05:00.484 Error: dzVents: Error: (3.0.2) sDate was invalid. Reset to 2020-10-31 14:5:0.320 
I expect this is because the alarmtime input is a string "Off" while it expects a time. Or did i miss something in the code of the alarm clock?

Code: Select all

-- Alarm clock [ dzVents >= 2.4 ]

scriptVar = '20191030 Alarmclock'

return 
{ active = true,
    on =
    {
        timer = { 'every hour' },    -- hourly run once to pick up target time
        httpResponses = { scriptVar }, -- Trigger the handle Json part
    },
    
    logging = { level = domoticz.LOG_DEBUG },

    execute = function(dz, item)
        local alarmTime = dz.devices('Wekker').levelName
        local minutesIn24Hours = 24 * 60
        
        local now = dz.time                             -- Get current time into time object
        local Time = require('Time')
        local targetTime = Time(dz.time.rawDate .. ' '  .. alarmTime .. ':00') -- Create target time object

        local function triggerAlarm(delay)
            local message = 'tring, trrring, trrring alarmclock ringing now. Was set on '.. dz.time.rawDate .. ', at ' .. dz.time.rawTime
            local url = dz.settings['Domoticz url'] .. "/json.htm?type=command&param=addlogmessage&message=" .. dz.utils.urlEncode(message)
            dz.openURL({ url = url, callback = scriptVar }).afterMin(math.floor(delay))
        end

        if not(item.isHTTPResponse) then
            dz.log('Alarm time is set at : ' .. alarmTime)
            local deltaMinutes = math.floor(now.compare(targetTime).minutes)
            if now.compare(targetTime).compare < 1 then -- targetTime is in future
                deltaMinutes = minutesIn24Hours - deltaMinutes
            end
            dz.log('Alarmactions will be triggered in ' .. deltaMinutes .. ' minutes' ,dz.LOG_DEBUG)
            triggerAlarm(deltaMinutes)
        else
            -- do wakeup actions.. here
            dz.log('wake up actions here....'  ,dz.LOG_DEBUG)
	    dz.devices('Kachel badkamer').switchOn()
            dz.devices('Kachel badkamer').switchOff().afterMin(15)
            dz.openURL('http://192.xxx.x.xxx:5005/Slaapkamer/volume/10')
	    dz.openURL('http://192.xxx.x.xxx:5005/Slaapkamer/unmute')
            dz.openURL('http://192.xxx.x.xxx:5005/Slaapkamer/favorite/ClassicFM')
	    dz.scenes('Awaken').switchOn()
        end
    end
}

Re: Setting starttime Switch

Posted: Saturday 31 October 2020 19:07
by waaren
Dutchsea wrote: Saturday 31 October 2020 14:18 I expect this is because the alarmtime input is a string "Off" while it expects a time. Or did i miss something in the code of the alarm clock?
That seems indeed to be the issue. Should be fixed in below modified script.

Code: Select all

-- Alarm clock [ dzVents >= 2.4 ]

scriptVar = '20191030 Alarmclock'

return
{ active = true,
    on =
    {
        timer = { 'every hour' },    -- hourly run once to pick up target time
        httpResponses = { scriptVar }, -- Trigger the handle Json part
    },

    logging = { level = domoticz.LOG_DEBUG },

    execute = function(dz, item)
        local alarmTime = dz.devices('Wekker').levelName

        if alarmTime ~= 'Off' then
            local minutesIn24Hours = 24 * 60

            local now = dz.time                             -- Get current time into time object
            local Time = require('Time')
            local targetTime = Time(dz.time.rawDate .. ' '  .. alarmTime .. ':00') -- Create target time object

            local function triggerAlarm(delay)
                local message = 'tring, trrring, trrring alarmclock ringing now. Was set on '.. dz.time.rawDate .. ', at ' .. dz.time.rawTime
                local url = dz.settings['Domoticz url'] .. "/json.htm?type=command&param=addlogmessage&message=" .. dz.utils.urlEncode(message)
                dz.openURL({ url = url, callback = scriptVar }).afterMin(math.floor(delay))
            end

            if not(item.isHTTPResponse) then
                dz.log('Alarm time is set at : ' .. alarmTime)
                local deltaMinutes = math.floor(now.compare(targetTime).minutes)
                if now.compare(targetTime).compare < 1 then -- targetTime is in future
                    deltaMinutes = minutesIn24Hours - deltaMinutes
                end
                dz.log('Alarmactions will be triggered in ' .. deltaMinutes .. ' minutes' ,dz.LOG_DEBUG)
                triggerAlarm(deltaMinutes)
            else
                -- do wakeup actions.. here
                dz.log('wake up actions here....'  ,dz.LOG_DEBUG)
            dz.devices('Kachel badkamer').switchOn()
                dz.devices('Kachel badkamer').switchOff().afterMin(15)
                dz.openURL('http://192.xxx.x.xxx:5005/Slaapkamer/volume/10')
            dz.openURL('http://192.xxx.x.xxx:5005/Slaapkamer/unmute')
                dz.openURL('http://192.xxx.x.xxx:5005/Slaapkamer/favorite/ClassicFM')
            dz.scenes('Awaken').switchOn()
            end
        end
    end
}

Re: Setting starttime Switch

Posted: Saturday 31 October 2020 23:01
by Dutchsea
Yes, no more error! Thank you.