Appdaemon apps.yml - if constraint

Hi all,

I’m converting to appdaemon (should have done it a long time ago now I see how easy it is together with docker-compose) and have a rather specific question.

I have a appdaemon class called RoomAutomation where I configure my various rooms.

For some automation, I’d like to add a constraint (not a constraint for the whole app like default appdaemon constraints, but rather one that I’d like to implement myself).

Is there an easy way to turn the yaml under the if part to a boolean?

slaapkamer_3:
      switch_to_light: 
        - slaapkamer_3_licht_1
        - slaapkamer_3_licht_2
      velux: slaapkamer_3_velux
      antimuggen:
        if:
          - states.calendar.regeling_lena_zomervakantie.attributes.message == 'Ronald'
        turn_on: 20:00:00
        turn_off: 07:00:00

you could use the exec() command and type python in that environment.

        if:
          - result = self.get_state('calendar.regeling_lena_zomervakantie',attribute='message') == 'Ronald'

Then use exec on that line:

exec(self.args['antimuggen']['if'][0])

Then access the variable that’s in that line

if result:
   ... etc...
1 Like

I had to do the following to make it work though:

def parse_if(self, statement):
      exec('global i; i = %s' % statement)
      global i
      return i

I don’t know why you’d need to make it a global. I would expect this to work if you are trying to not set the variable name in your if statement.

def parse_if(self, statement):
    exec('ret = %s'%statement)
    return ret

I then have a namenotfound error on ret.

I’m just happy it works! Thanks for your input

Hmm, that’s odd. Must be the nature of the environment. At least you got it working!