Pyscript: how to create a sensor

Hi,

I have a pyscript that scrapes some values from a website (from my solar inverter, e.g. current power).
Now, I would like to have this script work as a sensor inside hass so I can use it for other purposes.

I read through the documentation, but couldn’t find a way. So is that possible, and how?

Thanks!

Never used pyscript but I did write some python to run under Appdaemon to create some sensors from an API which might fit your purpose. See https://github.com/brewston/Home-Assistant-Strava

1 Like

Ok, so you just use ‘set_state’ to both create the state and give it a value?

# Values returned are in meters, but I wanted miles so divide by 1609
all_ride = round(int(r['all_ride_totals']['distance'])/1609)
# Build the HA sensor entity with the value returned 
entity = "sensor.strava_all_ride_totals"
self.set_state(entity, state = all_ride)

That seems pretty straightforward.

Yep. Don’t forget to edit apps.yaml.to tell appdaemon about your new code.

Something seems not to work. This is my ‘apps.yaml’ in /appdaemon/apps/

---
hello_world:
  module: hello
  class: HelloWorld

solis_scrape:
  module: solisscrape
  class: SolisScrape

this is the content of ‘solisscrape.py’ in the same directory:

import appdaemon.plugins.hass.hassapi as hass
import requests
from requests.auth import HTTPBasicAuth

#
# scrape the inverters web page
#
# Args:
#

class SolisScrape(hass.Hass):

    def initialize(self):
        self.log("Solis scrape started")
        self.run_in(self.update, 0)
        # update every minute
        self.run_every(self.update, datetime.datetime.now(), 60)

    def update(self, kwargs):
        self.log("Solis update started")

        # get the status page
        URL = "http://192.168.0.111/status.html"
        page = requests.get(URL, auth = HTTPBasicAuth('admin', 'admin'))
        # split it
        liste = page.text.split()
        # check were the interesting data is
        now_p_index = liste.index('webdata_now_p')
        today_e_index = liste.index('webdata_today_e')
        total_e_index = liste.index('webdata_total_e')
        # the values are always two indices behind, snip of excess "" & ;
        current_power = float(liste[now_p_index+2][1:-2])
        energy_today = float(liste[today_e_index+2][1:-2])
        total_energy = float(liste[total_e_index+2][1:-2])
        # Build the HA sensor entity with the values returned 
        entity = "sensor.solis_current_power"
        self.set_state(entity, state = current_power)
        entity = "sensor.solis_energy_today"
        self.set_state(entity, state = energy_today)
        entity = "sensor.solis_total_energy"
        self.set_state(entity, state = total_energy)

You see, I’ve borrowed a bit from @brewston :slight_smile:
But it seems the code only ran once, as I have the entities in the list, but the values are constant. I’m also not seeing anything in the log.
?

Did you look in the log tab for the add-on ? That’s where I did all my debugging. Anyway, I tried your code and noticed you are missing import datetime try adding that ?

Yeah - it is missing in your code, too. :slight_smile:
I’ve replaced

datetime.datetime.now()

with

"now"

same effect, but without the import. Now it works, thanks!