Trigger issue lights on...Appdaemon

Hi,

I have a door sensor (sensor.sensative_strips_access_control), which indicates 23/closed and 22/open.

I thinking something like this in my app:

import appdaemon.plugins.hass.hassapi as hass

class TestAuto(hass.Hass):

def initialize(self):
self.listen_state(self.LightsOn, “sensor.sensative_strips_access_control”)

def LightsOn():
if sensative_strips_access_control == 22:
self.turn.on(switch.christmastree)
else:
self.turn.off(switch.christmastree)

How should i do with the listen_state() and the trigger part? Do i need the give any more arguments?

This is my first app and just trying to grep the concept.

Thanks,

First off, it makes it easier to read if you do formatting (tripple backtics before and after code).

You can either just listen for any state change (as you do), and define the action in your function, or listen to a specific state change, and then trigger e.g. lightsOn if the state changes to 22, or trigger a different function (lightsOff?) if it changes to 23.

To test the state of something, you’d use self.get_state('sensor.sensative_strips_access_control'), but you pass that into your function from your listen_state. So you can just use new == 22 instead.

Keeping in line with your way of writing this, I’d do something like:

import appdaemon.plugins.hass.hassapi as hass

class TestAuto(hass.Hass):

    def initialize(self):
        self.listen_state(self.LightsOn, 'sensor.sensative_strips_access_control')

    def LightsOn(self, entity, attribute, old, new, kwargs):
        if new == '22':
            self.turn.on('switch.christmastree')
        else:
            self.turn.off('switch.christmastree')

I’ve had a bunch of help from people here over the last month or two since I started looking at python. Feel free to take what you can use; this part of my repo is only different ways of doing motion sensors to trigger lights.


A bit of explaining, if you use (self, entity, attribute, old, new, kwargs): as arguments for your function, then you automatically get that info passed from the entity that triggers. So it passes the entity_id (entity), the old state (old), the new state (new), as well as various other stuff (kwargs). That’s why we can use new == 22 instead of self.get_state('sensor.sensative_strips_access_control') == '22'; because a state change in sensor.sensative_strips_access_control is what triggers.


EDIT: corrected some formatting in the script

Aephir, thank you VERY much!
A lot of answers in that post.
I will take a look at your code!
Best
A

Sure. Note that I edited the script (made a couple of typos at first). I’m 90% sure it’s '22', but if that doesn’t work, try 22 (without the quotes).

@Aephir already did a good job helping with the first steps.

just such first steps i did explain here:

and i advise to read a lot through the docs, esspecially these parts:
https://appdaemon.readthedocs.io/en/latest/HASS_TUTORIAL.html
https://appdaemon.readthedocs.io/en/latest/APPGUIDE.html
https://appdaemon.readthedocs.io/en/latest/AD_API_REFERENCE.html
https://appdaemon.readthedocs.io/en/latest/HASS_API_REFERENCE.html

i know that quite a lot to read, but i assure you its worth every minute you spend there.