Setting starttime Switch Topic is solved

Moderator: leecollings

Post Reply
michelb
Posts: 1
Joined: Wednesday 30 October 2019 20:22
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Setting starttime Switch

Post 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 :)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Setting starttime Switch

Post 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 1212 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
pvklink
Posts: 822
Joined: Wednesday 12 November 2014 15:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest b
Contact:

Re: Setting starttime Switch

Post 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...
Raspberry (raspbian on rpi 3) , Domoticz Beta, dzVents , RFXtrx433e, P1, Hue, Yeelight, Zwave+, X10, ESP(easy), MQTT,Weather Underground, System Alive Checker, Domoticz Remote Server to RPI with Google Assistant,
Jablotron connection, Ikea
User avatar
Dutchsea
Posts: 120
Joined: Tuesday 08 December 2015 13:22
Target OS: Raspberry Pi / ODroid
Domoticz version: v2023.2
Location: The Netherlands
Contact:

Re: Setting starttime Switch

Post 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
}
Raspberry PI 3B on USB drive, Debian version: 12 (bookworm), Domoticz 2024.7
Aeon Z-Wave G5 stick, RFXCOM, Sonof Zigbee 3.0 Dongle plus E, Nefit Easy, Fritzbox, Buienradar, Sonos
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Setting starttime Switch

Post 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
Dutchsea
Posts: 120
Joined: Tuesday 08 December 2015 13:22
Target OS: Raspberry Pi / ODroid
Domoticz version: v2023.2
Location: The Netherlands
Contact:

Re: Setting starttime Switch

Post by Dutchsea »

Yes, no more error! Thank you.
Raspberry PI 3B on USB drive, Debian version: 12 (bookworm), Domoticz 2024.7
Aeon Z-Wave G5 stick, RFXCOM, Sonof Zigbee 3.0 Dongle plus E, Nefit Easy, Fritzbox, Buienradar, Sonos
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest