Yes that’s my mistake. I should have elaborated. I’ll edit the post for others.
How do I create a binary sensor that retains state after restart ?
Right now I do this:
action:
- service: python_script.set_state
data_template:
entity_id: binary_sensor.droogkast_actief
state: "off"
allow_create: True
This creates the sensor perfectly, but the sensor is unavailable after restarting HA until the variable is set. Is there a way the sensor remains alive (and retains value) upon restart ?
No, that’s exactly obe of the problems of the variables cuatom component. The only workaround I can think of is setting the variable when home assistant starts, but for this you need an input_helper that stores the value before the restart.
Personally I use MQTT sensors for stuff like this with the retain flag set to true.
how do you create and publish the value to an mqtt sensor for such use case?
just publish to the topic
thank you, but it would be great if one could share some actual examples for sensors
write an automation that does x y or z and publishes the data to a topic of your choice. Then create a mqtt sensor that reads from the topic. There are many examples of this on the forum, just not in this thread.
Which “variables custom component” are you referring to?
This one:
Actually with that one you can make the the state survive restarts with a “restore: true” in the variable config.
I use it all the time and it works fine.
EDIT: But now that you made me look at my variables I see that I’ve been bitten by the new template functionality…I think. I’ll have to look into that part but the save state over restarts still works fine.
I never used it, just saw someone mention in the past that they don’t survive restarts. Maybe it was a misinformation or functionality wasn’t there back then.
Thabks for the correction.
Hy rodpayne,
thanks for your solution.
I Create your file set_state.py end reload HASSIO.
Now i want call the service not from YAML script but from NODE-RED.
What kind of node i need to use for call that scrpt?
thank,
Marco
Sorry, I don’t use Node Red.
Here is another example where updating a sensor using python_script.set_state
may make sense. I wanted to use different icons for motion detectors on/off because the default ones look too much like each other. (The motion detector man just moves his leg.) The sensor does get updated by integrations that reset the icon, but when that happens another state change calls my automation again.
#----------------------------------------------------------------------------------------------------------
# Wyse Motion Sensor - make motion more noticeable with icons that don't look like each other.
#----------------------------------------------------------------------------------------------------------
- alias: Motion Detector On
initial_state: 'on'
mode: queued
max: 10
trigger:
platform: state
entity_id:
- binary_sensor.wyzesense_779b04a6
- sensor.garage_motion
- sensor.garage_motion_west
- sensor.garage_motion_overall
to:
- 'on'
- 'Detected'
for: 00:00:01
action:
- service: python_script.set_state
data_template:
entity_id: '{{trigger.entity_id}}'
icon: mdi:run-fast
id: e159cce9171a40058eb46118ff64f2ea
- alias: Motion Detector Off
initial_state: 'on'
mode: queued
max: 10
trigger:
platform: state
entity_id:
- binary_sensor.wyzesense_779b04a6
- sensor.garage_motion
- sensor.garage_motion_west
- sensor.garage_motion_overall
to: 'off'
for: 00:00:01
action:
- service: python_script.set_state
data_template:
entity_id: '{{trigger.entity_id}}'
icon: mdi:border-none-variant
id: a731ca6e306f4bb590e1f96ffa4da084
maybe describe your needs first.
In NR some things can be done different and easier way. You can create sensors and feed them with data, you can store data in variables which survive restart because are stored on disk. I’m almost sure you won’t call some hacky py script.
But first you need to describe what you need to achieve
I wouldn’t call python scripts hacky, they use the same method that node red uses to create fake sensors.
Great, it works, I set if for aqara motion sensor, to set state “off” after motion detected.
Main issue was when motion is triggered, sensor state keeps “on” for 2 minutes and then sets “off”
Thank you rodpayne
just curious how you help yourself.
Although you changed the state representation in HA, the motion sensor remains in active state for those 2 minutes. it means it will not react on motion during this time period.
I can’t get it work…
I am trying to set sensor value
target_dli:
friendly_name: Target DLI
value_template: 10
so that I could use it with input-numbers with slider.
In dev tool - Services
python_script.set_state
entity_id: sensor.target_dli
state: 9
nothing changed…