How to manually set state/value of sensor?

Out-of-the-box solution is with helpers. Thanks anyway. I’ll check the script.

In context of home assistant, helpers are input_* entities.

You can also try hass-setter – something I whipped up quickly as a proof of concept.

It is basically the same as a python_script calling hass.states.set, but it can be called through a service and it restores the previous values on restart.

It has the same drawbacks (minus state restoration) as using hass.states.set too. You can overwrite an existing sensor without knowing it, and you can’t change the friendly name or entity_id in the UI.

Out-of-the box solution is
Event with data -> automation rewriting data to input_* helpers -> template sensor with attributes based on input_* values. This is too much and complicated. With script or setter this can work like
Event with data -> automation setting sensor values.

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?