Switchbot controlling Aircon


#1

1) Give a description of the problem
Using a SWITCHBOT to turn aircon on and off at certain temps

2) What is the expected behavior?
when temp rises above x PRESS button IF button has been pressed in last x minutes and temp drops below y press button again

3) What is happening/not happening?
I cant finish the piston successfully

4) Post a Green Snapshot of the pistonimage

5) Attach any logs (From ST IDE and by turning logging level to Full)
(PASTE YOUR LOGS HERE BETWEEN THE MARKS THEN HIGHLIGHT ALL OF THE LOGS SND CLICK ON THE </> ICON TO FORMAT THEM)

This is an interesting one I am using a SWITCHBOT (device that presses a button on a command from IFTTT) to try to turn ON aircon and then off again at certain temps…
Now my aircon wall control on/off is the SAME button and it cant be known if its off or on (if that makes sense)
So I need a piston that says
if temp rises above x press the button
if temp drops below y press button again BUT only if the button was pressed in the last z minutes - which means the aircon is already ON (other wise the temp DROP press could actually turn it ON if it hadn’t already been pressed )
I really dont know how to progress this as you can see above I’m stuck LOL

if anyone has any brainwaves (or a more elegant solution ) I would be grateful

Thanks again !

Bill P


#2

You have too many IF’s.

First of all…do you have the IFTTT integration set up for WebCore and the maker Webhooks set up in IFTTT?
If not, I went through a whole tutorial on setting that up in another thread:

If you did, then we can work on what you’ve got here. first, you have no triggers. You have no lightning bolts next to any of your statements. Therefore, your piston will never fire.

Once you get that sorted out, we can work on getting the rest working. My advice would be to use a local variable set to Date/Time and set that variable equal to the system variable $now when you execute the IFTTT action. Then you can compare $now to your variable when the next action happens.

See if that helps you and report back if you still can’t figure it out.


#3

Your description under #2 and #5 differ a little in terms of when the ‘only if pressed’ condition applies, but here’s one approach off the top of my head that might help get you going in the right direction.

Edit: Ryan posted just as I was posting, but it appears his thought process in terms of using the Date/Time is similar.


#4

Only thing I would do different is not convert EPOCH time to a normal number. I would use the variable less than expression where.

$now
Is Less than
addMinutes(lastSwitched, maxTime)

That way, you don’t have to do the dirty epoch math. Just have to store the previous as a Date/Time variable. Otherwise it doesn’t work.


#5

I agree. I tend do the ‘dirty math’ in examples so that people grasp the calculation, but in this case your method is cleaner and equally as clear.


#6

Thanks all…sorry Im in Europe and didn’t see these till the morning :slight_smile:
IFTTT integrations all working well, its just the piston- I was struggling with the logic of how to progress it .

will try the great suggestions here and report back… though I have to confess EPOCH maths is a new one on me and I did maths to a high level at school LOLOL

where would this line go in the piston Ryan ?

“$now
Is Less than
addMinutes(lastSwitched, maxTime)”

Thanks again

Bill P


#7

BTHROCK
Hmm doesn’t seem to run…I noticed line 17 on mine now says invalid date when not in the edit page ?
I changed the temps slightly to try to force it to run
here are the screenshots
1st 1 is the invalid date lines , next the actual piston then the log

31 04


#8

I think this is a piston that can not be tested by pushing the Test button.
(It will only set the datetime variable during the daytime when the temperature rises above X)

If you really want to see the datetime right away, you can temporarily change line 27 to say:
IF Contact Sensor 1's temperature IS above 17°C
The Test button will then work, and the variable will be populated.
You can then return the code to what it was.


#9

Yes I did that exactly…temp is set to 25 in the piston and the ACTUAL room temp is 27 reported by the appropriate sensor.(that shows in the log) … could try even less but it hasn’t triggered at 25

Bill P


#10

The “$now Is Less than addMinutes(lastSwitched, maxTime)” would replace line 35. And don’t worry about epoch math. All my original example was doing was converting milliseconds to minutes; Ryan’s approach is better for this particular situation.

Be sure to note the emphasis in @wcmore line where he changes the ‘rises above’ to ‘Is’. Change that, save the piston, and click the test button. Assuming the conditions in the restriction are true (time and presence), the piston will run. You can change it back to ‘rises above’ once you’re satisfied everything is working and {lastSwitched} has been properly initialized.


#11

Thanks for this…
as a newbie to this , how can I easily replace line 35 with such a complex command …? I dont even know where to find those expressions :slight_smile:

Bill P


#12

You don’t have to replace line 35. What I originally posted will work; it’s just not as pretty. My suggestion is stick with that for now until everything else is working as you expect, then one of us can walk you through the steps.


#13

Understood…ok some progress… changing to IS fired the command to turn on…
I then re saved the piston with temps that would trigger an OFF (i.e. sensor reporting actual temp of 26 and changing the second statements trigger temp to is less than or equal to 26 degrees… nothing happens, it stays on when test is pressed after saving
Piston and logs attached…
again MUCH appreciated !


#14

I have not verified the math, but on line 35 you are referring to a variable that does not exist.
lastSwitchedOn
should be
lastSwitched


#15

not my referral…I copied the earlier post …

Should I change that variable?

Bill P


#16

I would just edit line 35 as seen above, and remove only two characters (On)


#17

That’s my typo … sorry


#18

No problem at all. Two heads are usually better than one!

The math here works well, by the way.
temp


#19

Just a note about testing, you can use other triggers that you know will work. For example, switch changes to ON. If the piston works with that then doesn’t with temp you can look at the device. I shorted durations and swap in virtual devices all the time, especially on Pistons which are designed to only run every couple of days. By the time it happens I might have forgotten I changed it!

And I still think the addMinutes function is a lot easier to use when your’re dealing with time. That is what it was designed for. Why deal with an extra expression if you don’t have to? The function would be

Variable $now is LESS THAN OR EQUAL TO addMinutes(lastSwitched,maxTime)

DONE! No muss, no fuss. Easy peasy lemon squeezy. Why deal with making sure your parenthetical is right and all that other crap?


#20

Thanks Ryan…are you suggesting that change in line 35 above?