How to manually set state/value of sensor?

What exactly are you trying to do that needs to set the state of a sensor? Please explain your usecase, I don’t get what you want to do.

What are you talking about? are you even reading what I’m writing? You don’t need any helpers. Go straight from your event to the python script.

- alias: Event automation to script
  trigger:
  - platform: event
    event: whatever_your_event_is
  action:
  - service: python_script.set_state
    data_template:
      entity_id: sensor.whatever
      state: "{{ parse out your data that you want here }}"

This is not out-of-the-box. This is not how it’s meant to be used. This is a hack. It works. Ok, thank you.

No one ever said it was…

This is exactly what pythonscripts were meant for.

This is not a hack. This is advanced configuration.

3 Likes

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:

1 Like

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.

A post was split to a new topic: How do I import excel 365 values one for each day?

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

1 Like

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.

1 Like

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.

1 Like

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