svenakela / ha

Code related to Home Assistant
MIT License
8 stars 1 forks source link

Sensor supposedly doesn't exist #1

Closed four2six closed 10 months ago

four2six commented 10 months ago

hi Sven , great work, sorry to bother:

I get the error that my sensor is not existing, but it is....

my settings:

AP_IP = '192.168.2.108'
MAC = '00000218D7873B17'
SENSOR = 'sensor.koln_rhein_wasserstand'
2024-01-06 13:24:10.283988 WARNING epaper_small_chart: ------------------------------------------------------------
2024-01-06 13:24:10.285343 WARNING epaper_small_chart: Traceback (most recent call last):
  File "/usr/lib/python3.11/site-packages/appdaemon/threading.py", line 1095, in worker
    funcref(args["event"], data, self.AD.events.sanitize_event_kwargs(app, args["kwargs"]))
  File "/config/appdaemon/apps/epaper_small_display.py", line 33, in generate_chart
    VALUES = (self.get_state(SENSOR, 'today')[HOUR_NOW::] + \
              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
TypeError: 'NoneType' object is not subscriptable

2024-01-06 13:24:10.285698 WARNING epaper_small_chart: ------------------------------------------------------------

templating

{{ states.sensor.koln_rhein_wasserstand }}

gives me

<template TemplateState(<state sensor.koln_rhein_wasserstand=817.0; state_class=measurement, latitude=50.93694929574385, longitude=6.963300159749651, unit_of_measurement=cm, icon=mdi:waves-arrow-up, friendly_name=KÖLN RHEIN Wasserstand @ 2024-01-06T13:18:49.865520+01:00>)>

any idea what's going on? It's happening with any sensor i choose.

svenakela commented 10 months ago

Hi @four2six, what a great idea to use the display for water levels!

The error is due to the sensor not containing the attribute today, a primitive list with hourly data. All Nordpool sensors (each market has its own sensor) contain two lists, today and tomorrow. Each list have 24 values and the data rendered is taken from these two lists. It happens here in the code.

My code is made specifically for Nordpool electric forecast prices, i.e. look ahead in time from now and onwards. But as long as you have a sensor with n number of values as a list you can throw pretty much anything in there.

I guess you are using some integration towards the https://wsv.de/ APIs? Is it related to this template? If you could provide me with some information about your setup I can try to figure out if there is enough data in the sensors somewhere to render a chart.

four2six commented 10 months ago

Hey thanks for your reply. The sensor is using the Pegel-online Integration.

It does have a history, but you're right, there's no array with values.

svenakela commented 10 months ago

I think I can make it for you, I've found historical measurements in the API that should be fine to digest. You don't even have to use a sensor, it can be fetched directly from the python code. But it would be nice to have the sensor in HA too I suppose to be able to have the data in a HA dashboard. I will have a look at it later, should be easy to make it into HA.

four2six commented 10 months ago

Thanks! Looking forward to it.

I just tried throwing SENSOR = 'weather.home' at it with, i think that is the met.no sensor. I changed line 33 to

        VALUES = (self.get_state(SENSOR, 'temperature')[HOUR_NOW::] + \
                  self.get_state(SENSOR, 'temperature'))[:24]

it does throw an error in line 33, TypeError: 'float' object is not subscriptable, so obviously it wants an array of strings?

svenakela commented 10 months ago

Thanks! Looking forward to it.

I just tried throwing SENSOR = 'weather.home' at it with, i think that is the met.no sensor. I changed line 33 to

        VALUES = (self.get_state(SENSOR, 'temperature')[HOUR_NOW::] + \
                  self.get_state(SENSOR, 'temperature'))[:24]

it does throw an error in line 33, TypeError: 'float' object is not subscriptable, so obviously it wants an array of strings?

No it has nothing to do with data type. Go back to my first reply, the sensor doesn't have the expected attributes.

svenakela commented 10 months ago

@four2six, I have added a new class for you. You have to read the config in the header to make sure you get what you want out of it. This class renders the Köln Rhein water measurement, but it reduces the data to every fourth value (The API responds with data quarterly).

The chart is rendered historically, i.e. the last value is to the most left side and everything else has already happened. Therefore, the max/min values are also historical. The config value LEVEL_NORMAL gives the possibility to remove empty space in the chart and everything above LEVEL_WARNING will be red.

image

The file is named epaper_small_display_wsv_wasser.py and you have to add this to apps.yaml:

epaper_small_chart_wsv_wasser:
  module: epaper_small_display_wsv_wasser
  class: SmallDisplayChartWsvWasser

EDIT: Forgot to mention, that the class creates a HA sensor for you. If you want it. And with that sensor you can use the data in "normal" HA as well.

image