No one ever said it was…
This is exactly what pythonscripts were meant for.
This is not a hack. This is advanced configuration.
No one ever said it was…
This is exactly what pythonscripts were meant for.
This is not a hack. This is advanced configuration.
I want to push event from iOS with Shortcuts. This event will have any data I want: from the phone or just dictated (speech-to-text works like a charm). This event will be pushed to HASS with HA app. And I want to set sensor state with this data.
Image this: I’m in a car. Have NFC sticker there. This sticker launches iOS shortcut to take my location, ask me about fuel or range and than takes it all and pushes it to HA app which handles the event, pushes it to my HASS and there I have a sensor with my car location, fuel, range and whatever. Is this really that strange to expect events be used as data source for sensor and not only as triggers for actions?
Then go through proper channels when making a requrest instead of complaining to random people on the forum. What good does that do?
Vote for the WTH here, event based sensors:
Thanks for this find. I’ll vote.
I wrote in this thread because you’ve said: “you can’t set states on sensors… period.”
Maybe I didn’t catch all context. Sorry.
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