Help simplifing a long yet repetitious piston


#1

1) Give a description of the problem
Need to simplify a repetitious piston

2) What is the expected behaviour?
To be smaller - I’ve kludged this together generally lacking knowedge

3) What is happening/not happening?
It’s massive!

4) Post a Green Snapshot of the pistonimage

5) Attach logs after turning logging level to Full

No logs, but this should keep it easier to get my point across… This is one piston that’s used for a single room’s temperature schedule, pretty much a basic, ugly list of what temperature I’d like to set the room to for the whole day.

Process is as follows:

  1. This script runs once per minute.
  2. DS18b20 probe reads the room temp (device roomtempsensor)
  3. Piston checks what temp it should be between 2pm & 3pm by checking the hourly variable (integer 1400_1500)
  4. If it’s too cold, sends command to my radiator TRV (device radiator) to open
  5. This fires up the central heating via a zwave boiler switch.

There’s loads of (horrible!) background ugly code and various other pistons so I can set the maximum temperature (ever), do a 30 min temp boost, disable the heating when the alarm is set, etc.

Anyhoo, my issue (as mentioned) is the size of the code. I was hoping someone might be able to suggest a way of turning this…

…etc (as you can imagine, there’s 24 sections). I did use variables as much as possible when I first horribly knocked together, knowing that when I’d sorted one room then I could duplicate the piston for the other rooms, just altering the RADIATOR and TEMP PROBE devices to the right ones for that particular room, and obviously adjust the hourly temp settings.

Again, totally aware it looks horrific, there are doubtlessly better ways of doing this (I’d love a decent UI/smartapp, but that’s down the line when I learn more). I just really needed to get something up and running at the time which was within my scope/skills.

Could anyone please assist? I’d love to get these pistons knocked down from 22 sections or whatever they are now!

Cheers !

REMOVE BELOW AFTER READING
If a solution is found for your question then please mark the post as the solution.


Help simplifing a long yet repetitious piston - Part 2!
#2

Since there is so much consistency, you could likely reduce it doing something like:

IF $hour24 is between 19 and 22  |  7pm - 10:59pm
Then 
    Use 18
Else
    IF $hour24 is 7              |  7am - 7:59am
    Then
        Use 18
    Else                         |  all other times
        Use 5
END

#3

Is it necessary to run it every minute? Why not set it up to run whenever the temp changes and then check what time it is.


#4

I was definitely going to mention that before this thread closed, but maybe every minute is only during his testing phase?

If @Djh_wolf really wants it on a timer, I would recommend reducing the frequency to something like every 15 min. (It’ll be much less taxing on your system)


#5

Sorry, bad example from me. There’s a lot more variation in temps, that was a bit of a boring room.


#6

Yeah, the 1 min is just for testing.

And I can’t have it on change of Temp, as the temp changes in 0.1 increments quite frequently.


#7

The hour 24 you mentioned gave me an idea.

At the moment the variables are called

1600_1700 (which is the variable integer for anything between 4pm and 5pm)

Sorry, not sure how to do this, but if I just changed the variables to be:
16 (instead of 1600_1700), then…
17
18 etc,

Then could I run a Piston every so often, but use the result of $hour24 to dynamically construct one of my repeating paragraphs? That probably sounds more complicated than it is.

For example.

Every 15 mins
If temperature is less than ($hour24)
Then
Open rad valvs
Else
Close valve

this, if 1725, would be “17”, so somehow the ($hour24) part would be populated with the valie of the variable called 17

Does that makes sense?

If poss, would cut out a hell of a lot of code!


#8

You can create a new mini piston for that.

Every 1 hour
    Set @global to X
END EVERY

This way, your monstrosity can simply refer to the current temp stored in the global variable.


#9

haha @ monstrosity…you’re really not wrong there…

For reasons too boring to mention, I’d like to avoid relying on an hourly piston to determine something.

But I’m currently attempting to figure out how to get something to spit out the result “17” if it’s “1756”…I am currently playing with it and searching

…progress! Managed to figure out how to store 'contact03" value as “21” if the time is 2129 (anything 21xx-2159).

Now I just need to figure how to use the value of the variable with the name “21”…


#10

Selecting your temperature setting might be a good place for a switch statement? You can use the fall through setting if you have a lot of the same value. Would look something like this:

Switch $hour24
case 0:
case 1:
case 2:
case 23:
  set tempSetting=5
  break
case 3:
case 4:
case 17:
  set tempSetting=10
  break
end switch

I didn’t fill in all the hours or the right values but hope you get the idea. One your temp setting is selected then you only need one logic block.


#11

Would it help if you defined the target temperatures in an array/list and used $hour24 as the index into it?


#12

I get the gut feeling this is probably the route to look into, but quite happy to admit I don’t understand it. Could you please elaborate ?


#13

I’ll have a look into what switch statements are, not sure at the minute… googling now…


#14

Actually, @orangebucket 's suggestion is better.

define
  string tempLevels="5,5,10,18,5"         <-- fill in complete list of 24 values
  integer tempSetting
end define

set tempSetting=arrayItem($hour24,tempLevels)

#15

trying this now, but can’t seem to save…


#16

Lists are more cumbersome to work with. You are putting the value in the name definition. All you can put there is the variable name and then you would have to write code assigning values to each index. Better to just use string as I gave in the example above.


#17

Apologies, I am trying, but I’m not familiar.


#18

Use the set variable task with the expression box like this:

EDIT: And no apologies necessary. Happy to help. We just never know what level of user knowledge we are working with. Always ask if advice isn’t clear.


#19

righto, thanks, getting there = )

I have the correct number/value getting pulled out (assuming it takes “ten past midnight” as “00”, referring to the first number in the list/array and “ten past 1 in the morning” as “01”, referring to the 2nd number in the list/array".

So… all good.I think. But now it’s obviously easier to look at a vertical list to alter temps, which at the minute I’m using by having a list of variables, i.e…

image

so it would be better (?) if those variables could be used to populate the list/array instead of a long string. If that makes sense. May sound redundant, but it’s easier to work with… still plugging. Much appreciate the help thus far, I’m sure it’s pretty easy for you guys, I’m just getting stuck in a little = )

Obviously it’s easier to


#20

You can do lots of different things but no simple way to keep the labels and still simplify your code. When assigning the value using an expression box, spaces are not significant so you can do either of these options to make it easier to count where you are at when editing:

Either will create the same result in the string and provide the same response using arrayItem.

EDIT: Sorry, I forgot to add the " " around the full string when changing to an expression. The pictures above do not work as is.