Just started out fiddling with appdaemon because I needed some python dependencies that HASS doesn’t have. FYI i have 0 python skills until today so be nice . So I have a script that stores 3 values that all look like this.
[[0, 3], [1, 17], [2, 31]]
How would I get this into homeassistant as a sensor. This is my bin collection days where 0 is this collection and its 3 days away, 1 is next collection that is 17 days away etc etc. So there are 3 of these, general waste, recycling and garden waste. I know the script runs as it prints my values in the AppDaemon startup.
A little nudge in the right direction would be greatly appreciated. I am running HASS on rpi3 and have the AppDaemon 3.x installed with the correct packages and config already.
You can use set_state() to create a sensor and put data in it. I’m not sure you could put a Python data structure in the state though - it is normally a string, so you may have to convert the structure to text.
Incidentally, my council publishes my bin collection days as a calendar, which I import into my nextcloud instance and get HA to remind me from that. A simpler option if its available.
Thanks for the information and direction you sent me in. datetime was an odd one but i now have it running without issue on AppDaemon startup.
The set_state seems to not error now but the sensor also doesn’t show in the front end, I have a separate class that sets the sensors to 99 to test on AppDaemon reload but they don’t show in the states tab in HA. Do i need to define them somewhere in the frontend? My code for that is below.
class Init_Bins(appapi.AppDaemon):
def initialize(self):
runtime = datetime.now()
self.run_in(self.blankSensor, 5)
self.log(self.name+" app started, bins set to 99")
def blankSensor(self, **kwargs):
self.set_state("sensor.next_bin_blue", state=99, attributes={"friendly_name":"Days till Blue Bin collection"})
self.set_state("sensor.next_bin_grey", state=99, attributes={"friendly_name":"Days till Grey Bin collection"})
self.set_state("sensor.next_bin_brown", state=99, attributes={"friendly_name":"Days till Brown Bin collection"})
Sorry about the questions, it’s day two of python learning along side AppDaemon, trying small steps but still my head feels overloaded.
the datetime function is a basic python function to get time from the system or to calculate with time or to set a time.
the import function is to get the datetime library into your own python code so you can use it.
then in my example i set the variable runtime to 00:00:00 and after that i use a run daily so that daily at 0:00:00 the sensor gets a new value.
you use a run_in and set that to 5
so that means that after the code is initialised (first time run) it does run the callback(blansensor) 1 time 5 seconds after you started the app.
if the sensors dont show up i doubt if you announced the app in apps.yaml, so the app is never run at all.
(or you might have the wrong appdaemon version, or you might have to restart appdaemon)
so first we have to make sure that the code runs at all.
how did you nstall appdaemon and which version?
did you get the hello world message from the hello world app?
if so, after you created the code did you add the app to the apps.yaml?
if so did you get the log entry app started, bins set to 99?
its no problem that you are learning. you already came a long way.
we just need to figure out the blanks that you have so that we can get you on your way.
Thanks for being so understanding! Ha ha the issue I had with datetime was cause I imported a library that made it not function correctly.
The AppDaemon is the version 3 one from HASSIO. My default helloworld app runs and i get the message Hello from AppDaemon indicating it’s working. My code is set to run in the apps.yaml and it ran so i got the message init_bins_days app started, bins set to 99 but i get the following error immediately after that (didn’t show before when testing sorry).
2018-02-04 05:02:15.018300 WARNING AppDaemon: Traceback (most recent call last):
File "/usr/lib/python3.6/site-packages/appdaemon/appdaemon.py", line 506, in worker
funcref(self.sanitize_timer_kwargs(app, args["kwargs"]))
TypeError: blankSensor() takes 1 positional argument but 2 were given
I am unsure what 2nd agruement i am passing as you can see in my code above? I have even changed the line def blankSensor(self, **kwargs): to def blankSensor(self): and no joy still. I have removed the line runtime = datetime.now() as this was unused but not sure why that now makes this error show.
i didnt know you had the beta version (v3) installed.
my tutorial is written for the AD version 2
almost everything from it is correct, but you need to change a few things.
the line at the top:
import appdaemon.appapi as appapi
needs to be changed to
import appdaemon.plugins.hass.hassapi as hass
and then the line:
class your_class_name(appapi.AppDaemon):
now needs to be:
class your_class_name(hass.Hass):
the apps.yaml should not be in the config dir anymore, but it needs to be at the same place as your app.
the advantage you get is that you can give the yaml file eny name you like.
i wonder though, how it is possible that your app did run anyway. but just let us forget that for this moment.
the error you get is because the callback sends 2 arguments, and you used the line:
Amazing! I read about the change to import appdaemon.plugins.hass.hassapi but assumed it didn’t affect my version as it was still working in some form. Everything you said, I have done and now it works like a charm. You got me to where I wanted to be, now I can fiddle with the sensors and make them perfect! @ReneTode you sir are a saint for all your work in this community!