Page 1 of 1

How do I check if a value excists?

Posted: Tue Jan 20, 2015 8:07 am
by Mastiff
I'm workikng on an if statement to check if the freezer or fridge are too warm or selected rooms are too cold (basically the rooms with water in them). The statement is pretty simple (all variables are pre set in other scripts, I have put explanatory names instead, and I just made the action a print for now):

Code: Select all

if eg.globals.Romtemperatur[Rom] < eg.globals.Alarmtemperatur1[Rom]:#For rooms with water in them
    print Rom, eg.globals.Termostatmodus[Rom], eg.globals.Alarmtemperatur1[Rom], eg.globals.Romtemperatur[Rom]
    eg.globals.WebsiteAlarmTemp[Rom] = eg.globals.Alarmtemperatur1[Rom]

if eg.globals.Romtemperatur[Rom] > eg.globals.Alarmtemperatur2[Rom]: #For fridge, freezer, cooler room
    print Rom, eg.globals.Termostatmodus[Rom], eg.globals.Alarmtemperatur2[Rom], eg.globals.Romtemperatur[Rom]
    eg.globals.WebsiteAlarmTemp[Rom] = eg.globals.Alarmtemperatur2[Rom]
It errors out because for the first type of sensors there is no max temp, and for most of the second type of sensors there is no min temp. I would like to keep it in one script, since the fridge has both a minimum and maximum temperature. So I'm looking for something similar to what I would have done in LUA:

Code: Select all

if eg.globals.Romtemperatur[Rom]: if eg.globals.Romtemperatur[Rom] < eg.globals.Alarmtemperatur1[Rom]: 
This code should theoretically first check if there is a value in the variable if eg.globals.Romtemperatur[Rom], and if there is it then checks if that value is smaller than the alarm temperature. Of course it errors out since it's partly LUA code. :wink: I could of course set alarm temps so far outside the possible that they wouldn't react (like a min temp of -50 degrees for the freezer), but that would mess up the second line in the script, which shows the alarm temperature for a room in my webserver, where I would like to have only one colum for alarm temps. Anyway, I'd like to learn how to check if a variable is empty or not.

Re: How do I check if a value excists?

Posted: Tue Jan 20, 2015 8:59 am
by Pako
You can use the following principle:

Code: Select all

if 'Romtemperatur' in eg.globals.__dict__:
    if eg.globals.Romtemperatur.has_key(Rom):
        test = eg.globals.Romtemperatur[Rom]
Pako

Re: How do I check if a value excists?

Posted: Tue Jan 20, 2015 9:48 am
by Mastiff
Thanks, Pako! Leave it to me to ask for the wrong variable... I need to check if the Alarmtemperatur1 and Alarmtemperatur 2 are there, not the Romtemperatur... Anyway, I got the principle, and this code works for me:

Code: Select all

if 'Alarmtemperatur1' in eg.globals.__dict__:
    if eg.globals.Alarmtemperatur1.has_key(Rom):
        if eg.globals.Romtemperatur[Rom] < eg.globals.Alarmtemperatur1[Rom]: #Frost guard temperature
            print Rom, eg.globals.Termostatmodus[Rom], eg.globals.Alarmtemperatur1[Rom], eg.globals.Romtemperatur[Rom]
            print('Alarm! Too cold!') #Print result for debugging
        if eg.globals.Romtemperatur[Rom] > eg.globals.Alarmtemperatur1[Rom]:print('No frost here!')
I have another one for the cold rooms, and that work as well.