Pass Sensor value to Python Script, process it and return back as another Sensor value

It’s no longer called hass.io and was renamed to Home Assistant OS which hides its containers.

In Portainer, go to Settings > Hidden containers and remove any filters that you may find there.

After you’ve cleared any filters, the Containers list should show all docker containers related to Home Assistant (and all Add-ons).

The one we are interested in is this one:
Screenshot from 2020-09-09 15-24-21

Click the >_ icon and you will be presented with a page where you can connect to the container’s console where you can enter pip install python-aqi to import the library. Remember, containers are disposable, so when you upgrade, this container will be discarded along with the python-aqi library. After upgrading, you will have to re-install the library.

Yes, I managed to get Containers displayed only after I unhid Hassio Supervisor.
‘Homeassistant’ container was inhid, however it’s still not displayed in Containers list.
Is this a bug of Portainer?

I installed ‘python-aqi’ library as you suggested in ‘Hassio Supervisor’ Container, though in fact, nothing changed. Scripts report that ‘aqi’ is still not found.

UPDATE: I unhid all Containers, then only ‘homeassistant’ container appeared in the list (maybe there are some dependencies):

pip install python-aqi

successfully,

Now it’s working! Thank you! I will try to pass the variable now.

If you re-read my posts, you will see that I never suggested to install it in that container. I said to install it in the homeassistant container.

I’m happy to hear you were able to install it in the correct container and it works. I have confidence that the next step, involving the command_line sensor, will also work correctly.

When you have a moment, please mark my post with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic now has an accepted solution. It will also automatically place a link below your first post that leads to the solution. All of this helps other users find answers to similar questions.

Thanks! I did it!

Sensor now works great!

1 Like

Update:
Installation of Python Library in HA via Portainer works only before next reboot.
After reboot you need to reinstall again… Some kind of inconvenience.

That’s exactly what Taras told you already:

That’s also why I recommend using AppDaemon for python programs as these changes will not be gone after a reboot/update.

I’m not seeing that behavior. I have it installed in my test system which is turned on only when I need it. The docker containers are regularly started and stopped. None of that affects the ability to use the python-aqi library.

The library is installed in the homeassistant container. If that container is replaced (such as after an upgrade or on demand to re-create the container from its image) then the library will be eliminated. Otherwise, a simple stop/restart of the container (such as caused by a reboot) won’t lose the library.

Sorry, maybe it was after I updated HA version. You are right, reboot causes no problems.

Would you please advise on how should I denote several arguments to my script that looks like this now?:

import sys
import aqi
pm25 = str(sys.argv[1])
pm10 = str(sys.argv[2])
no2 = str(sys.argv[3])
co = str(sys.argv[4])
myaqi = aqi.to_aqi([
    (aqi.POLLUTANT_PM25, pm25),
    (aqi.POLLUTANT_PM10, pm10),
    (aqi.POLLUTANT_NO2_1H, no2),
    (aqi.POLLUTANT_CO_8H, co)
])
print(str(myaqi))

In ‘configuration.yaml’ I tried

command: "python3 /config/python_scripts/py-aqi.py {{states('sensor.sds_p2')}, {states('sensor.sds_p1')}, {states('sensor.sds_no2')}, {states('sensor.sds_co')}}"

and

    command: "python3 /config/python_scripts/py-aqi.py {{states('sensor.sds_p2')}  {states('sensor.sds_p1')}  {states('sensor.sds_no2')}  {states('sensor.sds_co')}}"

While normally I pass like this when I test the script (using spaces)

python py-aqy.py 1 2 3 4

Why did you use double braces {{ in some places and a single brace { in others?

You should be using double braces only. They serve to indicate that the enclosed expression is a Jinja2 template. Single braces are for an entirely different purpose that’s not applicable in this case.

In addition, the arguments should be separated by a space character and not a comma.

I used like this:

    command: "python3 /config/python_scripts/py-aqi.py {{states('sensor.sds_p2')}} {{states('sensor.sds_p1')}} {{states('sensor.sds_no2')}} {{states('sensor.sds_co')}}"

But sensor value is ‘unknown’.
Double braces and spaces, as you suggested.