A newbie question about AppDaemon Global Variables

Been reading up on AD and I cannot seem to figure out if there is a way to manage global variables ?

my idea is fairly simple, at least in my head.

I want to be able to count a global variable up in each room for each person entering the room.
and count the global variable down for each person exiting the room.
if the global variable goes from 0 to 1 it should turn on the lights in that room.
if the global variable goes from 1 to 0 it should turn off the lights in that room.

I think I can do most of this easily in AD except the global variable part. ?
any idea ? or am I wrong in assuming it should be a global variable ?

I want to reuse the same code in all the rooms which I’ve already experimented with a little bit and that seems to work.
My intention is to use triggers and the order in which they are triggered to determine if a global variable should go up or down.

Hello @Helbo15,

The easiest way to do this? And I am assuming when you say global variable, you mean you want to be able to access the count from any app, is to create entities which will keep the state of those room, which is essentially the number.

So within AD, create a sensor say sensor.living_room_count. Now you can have an app that counts those entering and living the living room, and update that sensor.

Next have another app, which listens to that sensor, and when it goes above 0, which you Simply do by adding the old=0 flag when listening to it, it will activate what ever you like. Same goes for when you want to check, if it moved from 1 to 0, but this time you use new=0.

This way, it should work great


Thank you so much this was the information I needed.
it seems a lot to make an entire app to store variables global to other apps but it does make sense given the structure of AD.

Hello @Helbo15,

Actually there are other ways you can do the same thing to be honest. I suggested this one, as this way you can make use of the event mechanism to create a more will I say AD style code.

Other options are self.get_app, which will allow you to access the variables in other app from other apps. You can use self.global_vars, which is a variable that can be accessed across all apps. It’s a dictionary, so appA stores data in it, that appB can access it.

Others are just using plain events firing and listening, but this is more advanced stuff. Hope the info helps?

1 Like

Okay so depending on my needs in any specific case I can make the choice between an actual app to store global variables and actual global dictionary or individual apps variables…

Am I wrong to assume that making it an app would allow me to more tightly control which apps have access to the variables. or is it only to make advantage of the event system you suggested the app ?

Making it an app, and I guess by this you mean creating sensors is my preferred way as you said making good use of the event mechanism and it can be used for better control.

1 Like

yes I should had been more clear.
I can see why it is your preferred way.

I appreciate that you told me of the alternative ways to do this.

I think with my purpose in mind that creating sensors is the best option forward. but I am also happy you provided me with the other options as I might need that for other things at some point and as a minimum I am now aware of them and will be able to chose accordingly later on.

I’m trying to make an app that references a variable from another app using the self.global_vars method

Try as I might, I cannot seem to get the variables to populate across apps.


self.global_vars = { key : value, key2 : value2 }
I can then reference this from within the same app but when I try to reference it from another app I simply get an empty dictionary. What am I doing wrong?

well I’m finally back to this idea after a long period.
I however seem to have a problem with figuring out how to count the new sensor up or down.

I got two sensors at the door.
motion sensor A and B
if Motion sensor A triggers then B triggers I want it to count the Kitchen sensor one up.
if motion sensor B triggers then A triggers then I want it to count the kitchen sensor one down. how is this possible ?
and I want both sensors to reset their status once they counted the kitchen sensor up or down.
so I can have multiple people go through right after each other without the sensors not picking them up.

this is where I’m At ATM but I don’t feel confident that it would work:

import appdaemon.plugins.hass.hassapi as hass

class KitchenCount(hass.Hass):
    def initialize(self):
        self.log("Method initialize was called in the class KitchenCount")
        self.EntrySensorFirst = False
        self.ExitSensorFirst = False
        #self.persons = 0
        #belov I can make a sensor and set the initial state This allows me to use the sensors state to determine the lights state
        self.set_state("sensor.people_in_kitchen", state = 0, attributes = {"friendly_name": "kitchen people counter"})
        trigger = self.args["sensor"][0]
        trigger2 = self.args["sensor"][1]


    def EnterKtichen(self, entity, attribute, old,new, kwargs):
        self.log("Method EnterKtichen was called in the class KitchenCount")
        if self.ExitSensorFirst == False:
            self.log("EntrySensorFirst was first in the class KitchenCount")
            self.EntrySensorFirst = True
            self.log("1 Person Exiting Kitchen")
            kitchen_count = self.get_state("sensor.people_in_kitchen")
            self.log("kitchen_count is :" + kitchen_count)
            new_kitchen_count = ktichen_count -1
            self.log("new_kitchen_count is :" + new_kitchen_count)
            self.set_state("sensor.people_in_kitchen", state = new_kitchen_count)
    def ExitKtichen(self, entity, attribute, old,new, kwargs):
        self.log("Method ExitKtichen was called in the class KitchenCount")
        if self.EntrySensorFirst == False:
            self.log("ExitSensorFirst was first in the class KitchenCount")
            self.ExitSensorFirst = True
            self.log("1 Person Entering Kitchen")
            kitchen_count = self.get_state("sensor.people_in_kitchen")
            self.log("kitchen_count is :" + kitchen_count)
            new_kitchen_count = ktichen_count +1
            self.log("new_kitchen_count is :" + new_kitchen_count)
            self.set_state("sensor.people_in_kitchen", state = new_kitchen_count)

you will need to save the times that the motion is triggered and compare that.
the best way is probably to use 1 callback for both sensors.

something like:

def cb(...):
   if entity == self.args["sensor"][0]:
     self.a_triggered = datetime.datetime.now()
     self.b_triggered = datetime.datetime.now()
   if self.a_triggered < self.b_triggered:
     # a triggered before b
     # b triggered before a

sounds like a good idea just one thing I can’t see.

How do you prevent it from triggering if only one of the Sensors ever get activated ?

basically someone passing past the door but not going through the door.

i just answered what you asked :wink:
i have no idea how you placed your sensors, and what type of sensors they are.

in most cases its very hard to impossible to count entries with just motion sensors.

but if you placed them correct and they are from the good type, then its possible.

a simple addition to my example would make that it doesnt trigger twice.
and it could be made even more simple.
something like:

def init(...):
    self.last_seen = None
def cb(...):
    if self.last_seen != None:
        # first time triggered
    elif entity == self.args["sensor"][0] and entity != self.last_seen:
        # a triggered after b, counter down
    elif entity == self.args["sensor"][1] and entity != self.last_seen:
        # b triggered after a, counter up
    self.last_seen = entity
1 Like

I figured out how to make this work. but I cannot make it work consistently I think my problem seems to be the inability to quickly enough reset the motion sensors and their variables.

I can count up and down there was a few small mistakes in my code but just small blunders essentially it works once those were cleared up.

I guess I need a specialised sensor in order to actually do this.

Anyone knows what kind of sensors I could buy to make this work ?
I was using Philips Hue motion sensors. but I’m guessing most motion sensors will have the same issue so are there any specialised people counter sensors out there ?

so if you say motion sensors are hard. which I found out the hard way :wink:
what kind of sensors would you suggest is ideal for count up and down depending on entrance and exit ?

ideal is nothing.

i guess the best way would be a distance sensor on both sides from a door.
that way you can detect if someone is going out or in.
but the difficulty is that also the door itself triggers the sensors, so youll need an app to connect the 2 sensors to 1 sensor.

or any kind of motion sensors that only trigger on a very small area. and then also on both sides from the door.

in most cases its not detection motion that is hard, but determining in which direction the motion goes.
a distance sensor pointed at the door can do that, but you would need a place to place the sensor directly accross the door.