DIA Device - Alarm Clock

From Digi Developer

(Redirected from Dia Device - Alarm Clock)
Jump to: navigation, search


Alarm Clock Device

The Alarm Clock device is a low-speed general resource which can help other devices accomplish simple timed actions. It is designed to work with minutes or hours. Users who needed timed behavior faster than once per minute should use their own thread and timer logic.

At present it only offers:

  • the ability to trigger a transform (or publish a set) every:
    • 'minute' = once per minute, when seconds = 0
    • 'hour' = once per hour, when minutes & seconds = 0
    • 'six_hour' = once per 6 hours, so at 00:05:00, 06:05:00, 12:05:00 and 18:05:00
    • 'day' = once per day, so at 00:00:00 / midnight
  • it can print the line "{dev_name}: time is now 2009-05-31 10:47:00" at any of the above time intervals.
  • add a cron-like ability for other devices to request publish/sets at times such at 3:27am each Tuesday.
  • consider adding a second 'helper' thread to run complex/long jobs. For example STMP email service might take longer than 1 minute to return control to the alarm_clock thread.

Connections - Inputs

There are currently no block inputs.

Connections - Outputs

These are the block outputs.

Name Type Description
minute tuple Published once per minute (will always be True)
hour tuple Published once per hour on the hour (will always be True)
six_hour tuple Published once per 6 hours, at 5 min after the hour (will always be True)
day tuple Published once per day, at 10 minutes after midnight (will always be True)
  • The tuple sent is of form (time(), tuple(localtime(time())).
  • It is converted to a tuple since the same object reference is passed to all subscribers.
  • An actual example is: (1244544360.0, (2009, 6, 9, 10, 46, 0, 1, 160, -1))
  • Your device might MISS sets/publishes - for example another task might cause the alarm clock to be busy LONGER than 1 minute. So you device should examine the TIME value in the set/publish to determine how many seconds have really occurred since the last event.


These are the block settings which affect operation; some should be saved to NVRAM while others are dynamic.

Name Type Default Lifespan Description
tick_rate int 60 NVRAM seconds between ticks - change only for debug purposes!
printf str 'minute' NVRAM if and when alarm_clock prints a 'time is now' line in trace, in set ['none','minute','hour','six_hour','day']
  • Changing tick_rate does NOT affect the minutes etc, as those are based the real time clock; this only affects how often the thread wakes. Setting it to 15 means the thread wakes up 4 times for even 'minute' event. Setting it to 300 means 4 of 5 minute events are missed.

The Python Code

I place this in my src\devices directory. However, it has few dependencies and could be placed anywhere (it affects the YML form).

YML Example

This example shows the alarm clock device being named 'tick_tock', plus a second device which subscribes to tick_tock.minute, which allows that device to run once per minute without requiring a thread of it's own. The HourMeterBlock is NOT part of DIA - it is a custom user-defined device.

  - name: tick_tock
    driver: devices.alarm_clock_device:AlarmClockDevice
        printf: minute

  - name: motor_01_hours
    driver: devices.blocks.counter_device:HourMeterBlock
        active_true: True
        input_source: motor_01.channel1_input
        tick_source: tick_tock.minute
Sample output

This shows the once-per-minute printf with AIO adapters sleeping for 2 minutes at a time.

tick_tock: time is now 2009-06-10 09:23:00
XBeeAIO(smart01): AD0=0000 AD1=0000 AD2=0000 AD3=0000 bat=okay
XBeeAIO(itank03): AD0=0501 AD1=0000 AD2=0000 AD3=0000 bat=okay

tick_tock: time is now 2009-06-10 09:24:00
XBeeAIO(itank01): AD0=0153 AD1=0000 AD2=0000 AD3=0000 bat=okay
XBeeAIO(ssi01): AD0=0087 AD1=0000 AD2=0000 AD3=0000 bat=okay
XBeeAIO(itank02): AD0=0502 AD1=0000 AD2=0000 AD3=0000 bat=okay

tick_tock: time is now 2009-06-10 09:25:00
XBeeAIO(smart01): AD0=0000 AD1=0000 AD2=0000 AD3=0000 bat=okay
XBeeAIO(itank03): AD0=0501 AD1=0000 AD2=0000 AD3=0000 bat=okay
Python Code


Dec-2009 Enhancements - Events

In the Dec 2009 version I have added the ability to create time-based boolean event channels. So for example, you could create a channel named 'xmas' which is True from 6PM until 10PM, and False the rest of each day. Needless to say, you could subscribe to such a channel from a Digi SmartPlug, and use it to turn On/Off your outdoor Christmas lights.

New Details

This new (Dec 2009) version allows an optional 'action_list' section, following which you can add a number of events. Each event creates a NEW output channel on the AlarmClockDevice - so in the example below the new channel named tick_tock.xmas becomes available. In this example, the channel tick_tock.xmas will be True during the period from about 18:00 (6pm) until 23:00 (11pm).

The 'per' setting can also be set to 'hourly', in which case the 'on'/'off' should be minutes of the hour, so an output can be true at some arbitrary minutes-after-the-hour. Note that since one of my goals is to NOT load the CPU for unnecessary accuracy, it is possible the event triggers up to a minute late. So the outdoor lights might turn on at 6:01pm instead of 6:00pm.

My main TODO item is add the notion of which days of the week - if you look into the code you see that the 'do-daily' code is 0x007F, so 7 true bits. The design goal would be to enable causing the event to fire only once per week (say on Wednesday at 3am), or only have it occur on weekdays or weekends.

Example YML for Christmas Lights

  - name: tick_tock
    driver: devices.experimental.alarm_clock_device:AlarmClockDevice
        tick_rate: 60
        printf: minute
            - event: { 'nam':'xmas', 'per':'daily', 'on':'18:00', 'off':'23:00' }

  - name: xbee_device_manager
    driver: devices.xbee.xbee_device_manager.xbee_device_manager:XBeeDeviceManager

  - name: my_lights
    driver: devices.xbee.xbee_devices.xbee_rpm:XBeeRPM
        xbee_device_manager: "xbee_device_manager"
        extended_address: "00:13:a2:00:40:48:5a:65!"
        sample_rate_ms: 5000
        default_state: Off
        power_on_source: tick_tock.xmas
Python Code


Personal tools
Wiki Editing