How to debug / manually execute sensor?

Hi,

still pretty new to the concepts of HA, I am trying to monitor a web page for changes, specifically I want to know when a new firmware for my inverter is published.
I found some code in the net and pasted it to my configuration.yaml (60 seconds are just for testing)

 sensor:
  - platform: command_line
    name: sensor.sungrow_firmware_github
    command: >-
      python3 -c "import requests; response = requests.head('https://github.com/sungrow-firmware/firmware/tree/main/Hybrid%20Inverter/SH8.0RT-V112'); print(response.headers.get('ETag'))"
    scan_interval: 60

I tried the python code on the commandline of my linux workstation and it works.
However, nothing in log viewer and the device does not show up in developer tools.
How could I test the sensor, or maybe manually make it run? In other words: how to find out why the sensor is not showing up?
Best regards and thanks in advance,
Otto

That was your mistake. HA is fast-evolving. Always look at the official docs. In this case:

That would have shown you the correct way to write a command-line sensor on a current system:

command_line:
  - sensor:
      name: sensor.sungrow_firmware_github
      command: >-
        python3 -c "import requests; response = requests.head('https://github.com/sungrow-firmware/firmware/tree/main/Hybrid%20Inverter/SH8.0RT-V112'); print(response.headers.get('ETag'))"
      scan_interval: 60

And it works:

Thanks a LOT! Working now.
But my question stays the same, as I also want to learn how to find such mistakes in my config. In this case there was no error in the config, nothing in log viewer, it just did not work. How can I debug things like this?
Best regards and thanks again,
Otto

I added your original sensor-based code to my system and restarted. The log showed me this:

Not super-helpful, but that is the result of trying to load an unrecognised platform.

Hm. Thanks a lot. I saw these lines in my logs tough. But I could not see a connection to my sensor. Damn.

1 Like

Just one more question.
I added an automation for this sensor if it changes. Now this automation runs every time I am restarting HA. (how) is it possible to make the value of a sensor like this “persistent”?

It’ll be running because your command-line sensor is changing from unknown or unavailable (before it is initialised) to the ETag value.

You can set it to exclude those: see the final example of this section:

trigger:
  - platform: state
    entity_id: sensor.sensor_sungrow_firmware_github
    not_from:
      - 'unknown'
      - 'unavailable'

Thank you!
even worse. seems that the ETag value from github changes with every request. I think I will have to find a different solution…

Create a Scrape sensor via the UI Use my Rest solution below instead.

Scrape sensor details if you really want to see

Resource

https://github.com/sungrow-firmware/firmware/tree/main/Hybrid%20Inverter/SH8.0RT-V112

Select

script[data-target="react-app.embeddedData"]

Value template

{{ value_json['payload']['tree']['items']|list|count }}

That will (until GitHub changes its web architecture) count the number of files in that folder.

Wow! Thanks a lot! Just played around a little thing with wget and thought of doing something like that in a python script. But if this can be done within HA, it is much better :slight_smile:

Scrape is an inelegant and horrible solution, but works in this case without having to use anything outside HA.

If GitHub publishes some sort of JSON or XML listing, that would be better: you could use a Rest sensor to read that. Perhaps:

Yes. I know. scraping sites is a very lame thing. if the page architecture changes you are getting troubles… :frowning:
I have been scraping several pages ages ago. but since that dynamic stuff everything has become pretty difficult :frowning:

Solved it. Rest sensor (integration) like this:

rest:
  - resource: https://api.github.com/repos/sungrow-firmware/firmware/contents/Hybrid%20Inverter/SH8.0RT-V112
    scan_interval: 86400
    sensor:
      - name: "Firmware count"
        value_template: "{{ value_json|count }}"

If this is the first time you have used a rest: top-level heading, you’ll need a full restart.

:sunglasses:

haha. you are a genius! thanks a lot! :slight_smile: