Trying to use one function for 2 things with presence

So I originally had this in NodeRed but I was like why am I repeating this when I can just use one function in python and be done. So this is what I have

self.listen_state(self.welcome_home, "person.1", new="home", old="away", name="Dad",
                    constrain_start_time="sunrise",
                    constrain_end_time="00:00:01",
                    constrain_presence="anyone")
        self.listen_state(self.welcome_home, "person.2", new="home", old="away", name="Mom",
                    constrain_start_time="sunrise",
                    constrain_end_time="00:00:01",
                    constrain_presence="anyone")

# Presence announcements
    def welcome_home(self, entity, attribute, old, new, kwargs):
        self.log("Announcing home")
        message="{} is home" .format(kwargs(name))
        self.sonos_announcement(message)
        self.alexa_announcement(message)

But I’m getting this in the logs

2019-10-22 18:01:29.525777 WARNING AppDaemon: Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/appdaemon/appdaemon.py", line 595, in worker
    self.sanitize_state_kwargs(app, args["kwargs"]))
  File "/conf/apps/sonos_announcements.py", line 75, in welcome_home
    message="{} is home" .format(kwargs('name'))
TypeError: 'dict' object is not callable

Thinking now I can probably put both my wife and I in the apps.yaml and eliminate even more lines of code by just putting in a for loop instead of 2 listen_states. I’ll have to do that after I solve this.

Your syntax for reading a value from a dict is wrong. Try this.

message="{} is home".format(kwargs['name'])

1 Like

Thank you very much for your coding guidance. I’m still learning python (self teaching) but I’m getting better

You’re welcome. I suggest reading up on lists and dicts in Python and when you’re at it read about Python’s get method as well.

Will do. Also just a quick question, while this now works if I manually trigger the state of my person entity, when we get home its not triggering the announcement. I looked at my states and they seem to be correct with new=“home” and old=“away” since I definitely want to monitor those conditions. Just curious if something was changed in regards to the state that is being passed in. I can’t seem to find anything that says this won’t work as I have it set to do.

Are you sure that the old state is “away”, mostly it is “not_home”. Did you check on the state page?

When looking at your code, I think your “constrain_presence” is not needed, because the automation will anyway only fire when someone comes home, so the constraint is not needed at all.

1 Like

Can confirm, it should be “not_home”. HASS confuses the issue by showing “Away” in the GUI. But, as you said, on the dev-states page, you’ll see the true “state” is “not_home”.

Thank you @swiftlyfalling and @Burningstone for those tips. Gah the state page just reports it back as away so good to know that its not home. Also @Burningstone good point on the constraint worthiness. Also gave the items you suggested a read at W3Schools thank you for the suggestion greatly appreciate it and it will help me with my python for work as well :). I told them at work I’m a novice with it since I owe AD all of the credit for forcing me to learn it.

1 Like