# Setting temp by hour, works but not elegant

#1

1) Give a description of the problem
My electric company has a peak pricing plan, in summer the rate for 12-7pm is higher than other times. I am not charged by total usage, but by the highest demand in any one hour period.
My piston, allows the thermostat/room to slowly increase in temperature over time, to minimize the peak demand.

I am throwing this out, if anyone has ideas how to accomplish in a more efficient manner.

2) What is the expected behavior?

3) What is happening/not happening?
works but using somewhat of a brute force method. And time consuming to edit, IE when winter time has different times.

**4) Post a Green Snapshot of the piston!

#2

Depending on what time of day you last saved this piston, there is a 75% chance that no code will ever be executed.

The reason is, the â€śEvery 60 minutesâ€ť begins at the moment the piston was last saved. So, for example, if your last edit was at 2:45, then it will try to run at 3:45, 4:45, 5:45 etcâ€¦ Meaning the `condition` checks below will never be true.

#3

I saved it just after the hour, and the piston has been executing without fail, for the last few months, I was looking for a more elegant and efficient way to do it. As I have become more proficient in WC core I have gone back and rewritten a number of my pistons, and surprised that some of them actually ran.

#4

Excellent. I guess you wonâ€™t notice that issue until you edit the piston next.

One work-around to get it working year round, (no matter what time the piston was saved), is to change your `conditions` to:

``````IF \$hour24 is 12, then do X
IF \$hour24 is 13, then do Y
IF \$hour24 is 14, then do Z
``````

etc.

#6

Why is there a 75% chance that no code will ever be executed and why is that based on when you saved the piston? Shouldnâ€™t it just start a 60 minute timer and when the timer runs out, if the current time is within one of those hours, the piston will run? I have a piston for setting a Bitter Cold Flag (to reduce thermostat setback at night to help recovery the next morning) when the outside temperature is less than 19Â°F. Since there is no trigger for \$twcweather.conditions.temperature changing, I just evaluate the outside temperature every 5 minutes, but only in the 8pm, 9pm, 10pm, and 11pm hours (as different thermostats setback at different times). My code is running, but if I edit and save it, will it have a chance of never running?

#7

The timer runs every 60 min from the time the piston is saved, and if the statement is â€śIF time is between 12:00 and 12:15â€ť and if saved the piston at 30 minutes after the hour, it would run at 12:30, 1:30, etc and never execute.

#8

In your case, it will continue to run after saving itâ€¦ The only difference is (for example) in the past it might have run at :00, :05, :10 past each hourâ€¦ but after the save, it may run at :03, :08, :13 etc.

(the timer begins at the moment the piston is saved)

#9

If you clicked the test button, would that restart the timer?

#10

Not with the command:
`Every X minutes, do Y`
It starts the endless timer at the instant the piston is saved.

#11

Is there an elegant way to have the piston run once either when it is saved or when the test button is pressed? And then also have run run on an interval.

#12

If your interval is hourly, I like this method:

``````IF Time happens daily at (\$hour24+1)":15"
Then Log to console "Piston ran"
END IF

The real code goes here, outside the top IF `trigger`.
``````
• When you press Test, the real code is executed.
• Each hour, there is a log written, and the real code is executed.

Note: The `:15` can be any number from `:00` to `:59`

I believe that credit goes to @guxdude for that elegant formula.

Pro Tip:

Not that I am recommending this, but if you add any other `triggers` to this piston, then those blocks will usually be ignored at each hourly event (or Test button).

#13

raises hand

Just so I understandâ€¦ the code runs from top to bottom (naturally) so the first run sets the trigger for the next occurence of xx:15 AND executes the code, but subsequent runs are BECAUSE OF the trigger thus logging â€śPiston ranâ€ť, then running the â€śreal codeâ€ť. Yes?

Edit: The rocket launch thread is KILLER! Nice work all.

#14

Mostly right, but just to clarifyâ€¦ A piston does not execute when saved.
(Although it may schedule a wakeup, if itâ€™s appropriate)

This means the first execution will occur sometime in the next hour.
(or you can press Test at anytime to execute everything except for the log)

Thanks. That piston was a blast to create, with so many innovative elements tied together.

Edit five hours later:

I just noticed my unintentional pun. (a blast to create).
(I guess even my subconscious enjoys witty word play. LOL)