How to manually set state/value of sensor?

What about sensor template? How can I manually update value of a sensor template? Or how can event data update sensor?

you can manually trigger (or automatically with automation) the sensors to update themselves using the homeassistant.update_entity service. You still can’t replace the value and have it persist.

I can trigger an external event. In the event I want to pass some data. This event could be injected to MQTT and then it would be possible to use it’s data as sensor. But when I use HA api (like generating event with HA mobile app) my event will update input helper that can be used as a source for template sensor. Why do I need this additional input helper? This event should update template sensor value or attribute directly.
SET STATE button can do this. Why events cannot trigger sensor update with data from event?

You can, but you aren’t understanding my ‘you can’t’ statement. If you have a sensor that updates on it’s own through some other source, you can’t set the state and expect it to persist. It will always be overwritten when the source updates the sensor.

In your circumstances, you aren’t going to have a sensor source that is causing updates like @QbaF has.

You can do what you want using the python script. But it will create a new sensor that only updates with your automation.

EDIT: For further clarification… If you set the state of a non-existing entity, HA will create that entity. That entity will persist until shutdown. Upon reboot that entity will not exist until you set it again. This is the route you will be going if you update a sensor using an automation that triggers from an event.

Ok, I agree with you on the persistence. This is clear for me.

And as for the python script needed to update sensor state - this is my WTH moment.
I just don’t buy the need for all those input helpers while there is “set state” button available. But not as a service.

what input helpers? You only need a single python script and call the python script as a service. All of which is detailed above.

As for the reasoning, I cannot comment. I didn’t make the decision.

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 ?