It is not clear exactly what you are trying to retrieve from the entity, but to get a state, you just need one parameter - the entity id. To get an attribute, you need the enttity id and a named parameter attribute.
just to make it clear.
hassio is an operating system like windows or linux.
the documents you point to are the home assistant docs, and home assistant is a program like word, excel or powerpoint.
you run home assistant on the hassio platform.
i say that, to avoid misunderstandings in the future.
Where I am trying to set message to the name of a device_tracker, the get_state attribute is returning None.
If I remove the attribute from the get_state function, it returns the state correctly.
Do I have a syntax or format issue?
i would try to keep things a little more simple that makes logging more easy.
this line:
message = str(self.get_state(entity='device_tracker.' + self.args['devicetracker_home_status'], attribute='full_name')) + " has arived"
i would translate to:
device = 'device_tracker.' + self.args['devicetracker_home_status']
full_name = self.get_state(device, attribute = 'full_name') # no need to say entity = ...
message = full_name + ' has arrived' # no need for str full name can only be a string.
now you can log for device, full_name and message and see if all 3 are what you expect.
@ReneTode Thanks. Doing the variables as you suggest is the right way to code it. It makes it so much easier to read. Out of curisioty, did you see what I did wrong or is it not worth time. I cool either way.
BTW, here was my finale version:
with this i can see where it goes wrong.
and if i have time ill always will help.
keep your apps clean. dont start to create callbacks before the initialise. its also the way the app works.
when AD is started or when the code is changed the initialise is performed, and only on that time.
so thats always the start from your app.
follows from that. you got the get_state in your initialse now. so the value is set when the app starts, but wont get changed when the callback is activated
import appdaemon.plugins.hass.hassapi as hass
import json
import urllib
class arive_home(hass.Hass):
def initialize(self):
self.device_tracker = 'device_tracker.' + self.args['devicetracker_home_status']
self.listen_state(self.arive_home_callback,
self.device_tracker,
new="home")
self.log("Current State '"
+ self.device_tracker_full_name
+ " is "
+ self.get_state(self.device_tracker)
+ "'")
self.log("Automaintion has been initialized.")
def arive_home_callback(self, entity, attribute, old, new, kwargs):
self.log('callback started')
self.device_tracker_full_name = self.get_state(self.device_tracker, attribute='full_name')
self.log("Current State '"
+ self.device_tracker_full_name
+ " is "
+ self.get_state(self.device_tracker)
+ "'")
message = self.device_tracker_full_name + " has arived"
self.call_service("notify/"+self.args['send_alert_to'],title="",message=message)
self.log("\n****************************\n"
+ message + "\n"
+ "****************************\n"
+ "Message Sent")
and when changing this i noticed 2 more things:
dont use entity=… in get_state (like i mentioned before)
decide what you want to use for quotes. i see you sometimes use ‘’ and at other times use “” to create strings. pick 1 or the other and always keep using that. you will get crazy debugging at somepoint when you keep using both for the same purpose.
I did not mean the way it sounded, I was asking if it was worth either of our time. Looks like yes. I did learn a lot. Thanks.
I was trying to get in the “style” of hass code. Someday I would like to do a component. I thought the style was if one word, ‘’ if more than one word “”. Even at that I was not consistent. Thanks for keeping me honest.
Got it.
I had the message hard coded for what the event was going to be. I just reworked it, so now there will be one app with two apps.yaml definitions. I was using the developers state to change the device tracker entity. Right now I am away, so I was setting it to “home”. Then what confused me when the hass corrected the tracker on the next update, I was expecting the other to fire. Well I consolidated down to one app. In the course of debugging I notice the away is not “away” but “not-home”. That is why I was missing the other app.That is what prompted my about the question of instances. If I would have posted the “away” one instead of the “home” one you would have saw that. Everything behaves as I expected now. This is just a test to see when the “home” and “not-home” fires for future automation.
Here is the apps.yaml and “left_arived_home”
Well… might have spoke too soon.
I took off the new= from the listen.state. Now the event is firing about ever 5 to 10 minutes.
Can I set the new= to new={“home”,“not-home”}? How can I trap the event only when it changes?
the calback only fires when something changes.
but most devicetrackers in HA are quite independable. so you can get unwanted state changes when you have set the delay to short.
and it also triggers the callback when an attribute changes if you dont set new.
you can do 2 things:
use 2 listen_states 1 for home and 1 for not_home or
in your callback check for what you want it to be with if new=“home”:
Still now sure what I am doing wrong. The event is firing even if there is not a change.
I added to the log the current state, old state, and new state. If there was a state change you would have expected the old and new to be different. It was not.
Just to test my theory, I then forced a state change and the old/new was what I expected. I can use that now to keep false fires from happening.
Here is the log showing what I am talking about. https://hastebin.com/uqoqagusil.md
and the code that go with it https://hastebin.com/tevukumomi.py
I am going to add a test that old != new.
Will report back?
the code you have has a problem.
you have a callback that gives you new as current state and the you go check the current state with get_state.
but that is a fraction of a second later.
if those 2 are different, then the state is changed twice and you get the callback to times.
with some devices and some devicetrackers you get a short state change. thats why i said that devicetrackers are not dependable. (there are people combining 4 or 5 devicetrackers to get an accurate state)
attribute should contain all attributes. log those and see if anything changes there.