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