Clock and times! Day / night settings

Hi all,

I’ve been jumping on and off THR and think I have just about got to grips with everything I need it for however I can’t work out how to have certain icons change dependant on the time of day.
Basically all I want to be able to do is when it’s night time (say between 7pm and 7am) a certain icon to be used, then through the day (7am - 7pm) another icon to be used.
I am happy on how to setup the icons to change based on a devices state however have no idea how to either create a virtual device to be able to do this IE give a day / night variable or simply input these as a setter for the icon.

If someone can point me in the right direction or has an example of this I could look at I’d be grateful.

Many thanks in advance,


oh man… I just scrapped my first plugin attempt (it wasn’t going well for me lol) that spit out the current time. I was trying to create a clock for animations.

In addition to spitting out the current time, I was trying to spit out additional attributes that I could use for faster animations…
For example:
converting time into a 1 or 2 (to flip back and forth between two images every second)
converting time into a 1, 2, or 3 (to cycle through three different images every second)

Some of the smart guys around here could make a plugin with a getTime.


Another solution is if you have a Smart hub that you can create a virtual switch that turns on/off per schedule or sunset/sunrise. Pull that into THR and use it to change your icons. This is probably easier but I’ve been dying for animations so I like my plugin idea better lol

This would be a snap if you were open to using Node Red :slight_smile:

What’s coming to my mind is to create a flow that triggers at a time to set a value in a MQTT topic. Say a boolean for daytime. The THR MQTT implementation is really good, and would pick up that value which could be used as a condition in two Data Setters. One that sets the icon to the sun If TRUE, the other to set to the moon if FALSE. (Or whatever action you’re looking for)

Alternative is Home Asisstant. They have sunrise/sunset functions that could also publish MQTT messages.

It might be a little more overhead than desired (having to set up both a Node Red and MQTT Server). However, I find myself constantly using the two of them all the time, especially to overcome device limitations where they only accept one TCP client connection at a time (i.e. I set up a TCP listener on Node Red which acts as a multi-connection proxy), and use other functions to take the heavy lifting off of THR.

The last thing I did (tonight even) was to set up a flow that would proxy my video processor TCP connections. Using MQTT, I can now allow any device on my network to do a simple publish to a given topic to display messages on my projector.

Just thought about an easier way to do it, all within THR, while eating my nighttime bowl of cereal and banana (brain food).

I’ll try to whip something up tomorrow if someone doesn’t beat me to it.

My thinking:
In the OnPoll function of a plugin (either a new one or one that exists already)

var current= new Date()
var time_of_day=current.getHours()

if` (time_of_day >=7 && time_of_day < 19) {
//set device attribute to day image file name
} else {
//set device attribute to night image file name

You’d probably want to configure a new device attribute as well in the OnSync section.

Here’s a project with code that works :slight_smile:

DayNight.plugin (1.1 KB)

The one thing that isn’t implemented is adjusting for time zone (i.e. I was adjusting my TZ on windows and while the system clock showed the correct time, THR kept reading the original time. I’m sure it’s a simple fix, but I’ll leave that to someone else.

Otherwise, manually changing the time in Windows does yield the new hour.

DayNight.hrp (20.6 KB)


Sorry for my slow reply but that is spot on!
Just given it a quick test and works perfectly. As you said adjusting the windows time works for testing purposes.
Thanks again for this, time to have a bit of a play!

1 Like

No worries.

Just thinking about this…would it be helpful to have the day and night dynamic based on sunrise and sunset times? I found an open API that could be used.

Let me know…I like coding :slight_smile: