[SOLVED] Monitoring for a state unchnaged

no it will only say that the first time.
the first time you use set_state it will be created.

to avoid errors from the get_state you do want to add these lines to your init (so the sensor will be created everytime you restart HA and AD)

device, entity_name = self.split_entity(self.args["sensorID"])
if not self.entity_exists("sensor." + entity_name + "_last_updated"):     
    actualtime = datetime.datetime.now()    
    actualtimestr = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    self.set_state("sensor." + entity_name + "last_updated", state = actualtimestr)

Well isn’t that just amazing. So we basically create a little virtual sensor right here in the code. I like it.

yup. if you now look in your home assistant you will probably have more then 1 created (because you changed the naming)
but they will dissapear as soon as you restart HA.

i use it like this:

Yep you are absolutely correct it is now registered up in HA on my overview page as a sensor just chilling there.

if you dont like it at that place, you can put in in any group you like, just like with any other sensor.
and if you want another friendly_name for it you can do that like:

self.set_state("sensor." + entity_name + "last_updated", state = actualtimestr, attributes={"friendly_name": "any name you like and you can make it dynamic"})
1 Like