Price Sensor with attributes

Hey there,
I have got a little python script, which crawls a website to get the current price of one or more tech devices. I’m new to AppDaemon but this might be the right choice to get some more sensor information in homeassistant!?

What I have:
Currently I am using the command_line sensor to get the price into homeassistant.

What I want:
This is quite limited, so it would be nice to have the name of the device, the second highest price etc. as attributes. In that case I could get some more information, when I click on the sensor in homeassistant.

I know, that I could build a workaround and run the script via a cronjob and send json data via MQTT, but there must be a better solution, right?! So, it would be awesome if there is someone with an idea to get this done.

Thanks for your help! :slight_smile:

You could certainly create an App to do this, the App would be able to put all the attributes you needed into Home Assistant.

The only gotcha is that Apps aren’t meant to be kept running for a long time (e.g. the time to crawl a website might be many seconds) so it would be best practice to use a background thread to do that part.

Ah cool, so now I know that I am on the right track! :smile:

What do you mean with “background thread”? Is it possible to geht the App triggered via “sensor.time” e.g.:

if states.sensor.time.state[-1] == '5':
    crawl_price()

Yes, you would do something like that, the problem is that crawl_price() will tie up a worker thread for all the time it is running. Probably not a big deal if you do it once every 5 minutes and have plenty of threads configured for other activities, but it’s something to be aware of.

I finally came up with this little app, which save the information of a selected product to homeassistant. I’m open for improvement suggestions! :slight_smile:

@aimc, one feature request: it would be awesome to get the library prequisites (in this case: bs4 ) in the appdaemon.cfg or in a requirements.txt.

Looks great!

I’m not sure why doing imports elsewhere than the code itself would be an improvement - can you explain a little more?

sorry to not be clear:
My app needs the bs4 python module, so I had to install it via the docker container console with pip3 install bs4, which is not a very handy onboarding. I think it would be greate to have e.g. a requirements.txt file in the AppDaemon config folder where one can add modules needed for an app.

The requirements.txt file would look like:

bs4==0.0.1

And AppDaemon would have to run something like pip install -r requirements.txt at startup (here are the docs).

you have installed it.
requirements are very helpfull if you have apps that many other people use, but not for 1 person.
even with the amount of people that we share our apps with, an announcement that a certain thing needs to be installed is enough.
unless you want to make install scripts for every app.

I came across the same need today. I think this could be done a couple of different ways.

  1. Provide a hook into the startup sequence that lets people run some code. That way I can handle this myself if I wanted
  2. Mimic the REQUIREMENTS logic in HomeAssistant, if possible.

I don’t really agree with the not useful for 1 person. With docker, when it comes time to update, I just pull the latest, stop the container, blow it away and start up the new one. My “apps” are in a mount that is presented to the docker container.

Option #1 would allow a startup app to let me manage this. Yes, ugly and not very “sharable”, but it would be potentially quick to implement for AppDaemon. #2 I think would be much more desirable, but might take more effort to implement.

If you want to add packages to appdaemon in docker, you should just create your own dockerfile and include them there