Set sensor value with appdaemon: value_template problem

Hi!

I’m using appdaemon to set the value of a sensor.

sensor Pegel Leine:
  - platform: template
    sensors:
      pegel_leine:
        friendly_name: Pegel Leine
        unit_of_measurement: 'cm'
        value_template: 
        icon_template: '{% if is_state("pegel_leine_trend", "on") %}mdi:arrow-up-bold{% else %}mdi:arrow-down-bold{% endif %}'

When I set value_template: to nothing, the value gets set nicely, but I get a warning in the log.
When I set value_template: '' (empty string), appdaemon can’t set the value, but no warning in the log.

Is there a better way to do this? Or should the warning be checked?

Here is my appdaemon script:

import appdaemon.appapi as appapi
import requests
from bs4 import BeautifulSoup
import datetime
import time as tm 

class Pegel_Leine_Class(appapi.AppDaemon):
    def initialize(self):
        time = datetime.time(0,0,0)
        self.pegel_leine(time)
        self.run_minutely(self.pegel_leine, time)

    def pegel_leine(self, time):
        self.log('')
        self.log('-----------')
        self.log('Leine-Pegel')
        self.log('-----------')
        self.log('')
        start_time = tm.time()
        self.log('Read HTML file')
        file = requests.get("https://www.pegelonline.nlwkn.niedersachsen.de/Messwerte")
        html_text = file.text
                
        self.log('Soupify')
        soup = BeautifulSoup(html_text, 'html.parser')
        
        self.log('Extract value')
        start = soup.find('a', href="/Pegel/Binnenpegel/ID/280")
        pegel_leine_int = int(start.findNext('td', align="right").text)
        
        self.log('Set state')
        self.set_state('sensor.pegel_leine', state=pegel_leine_int)
        self.log('Pegel der Leine: ' + str(pegel_leine_int) + ' cm')
        self.log('Execution time: '+str(tm.time()-start_time)+' s')

@ReneTode - this has come up twice in the last day or so - did you ever figure out a way to do this as you pioneered this idea?

for sensors the best way is to forget about it in HA completely.
it gets created the first time a value is send with set state and then you get a appdaemon warning.
if its a sensor in HA the problem in most cases rises that the sensor is updating it in the tact HA sets.
so HA overwrites the values set from out of AD. sometimes visible, sometimes it gets struggling.

you can still add the sensor that doesnt exist in HA to groups (it will show up there as soon as AD sends a value)
and if i remember correct you also can customize a non existing sensor in HA without any errors.
the customizing then also gets done at the moment the sensor is created.

attributes can also be set from out of AD.

1 Like

I know that @turboc does this with his Battery Levels monitoring app and I had to create HA template sensors for it.

Tagging him in to see if he has an idea.

imageFIle="/local/fan_green.jpg"
self.set_state(Entity,attributes={“entity_picture”:imageFile)

It’s a little confusing because in the code you set the path for the image as /local/imagefilename

But you actually put the picture in the .homeassistant/www directory.

The other thing is you have to keep watching the icon picture and re-evaluate it or replace it if it changes. When HA updates, it overwrites the value we have in entity_picture so you have to have a listen_state for that attribute to catch it and replace it. That’s what I’m doing. I’m putting the finishing touches on an app that will do this type of thing to simulate custom icons in HA.

1 Like

thats why i try to avoid the sensor in HA itselve.