How to manually set state/value of sensor?

Yep and most people in this thread went a different route or were setting a completely new sensor that wasnt’ overwriting an existing state.

All sensors will update periodically, if you set a state on a sensor that updates on an interval, your set state will be wiped clean. Same goes for a sensor that is based on another sensor. As soon as that other sensor updates, it will push the value to the new sensor.

Why are you trying to match the sensors, what’s the endgoal here? You already have the other value

The goal - I started with my energy sensors in HA when power company meter was already counting for few years. Is, of course, not accessible for me online. So I would like one of my entity has this same value as this power company meter to follow it. So I have to set initial value for my entity counting total energy consumed.

BTW. I can set up value for a sensor that is based on another sensor. Utility meters. But all of utility meters are resetted earlier or later.

Hey great discussion and great script.

I thought I’d add a use case where I’m actually updating the value of a sensor that will be overwritten. I have an integration with an Envisalink board to my Honeywell Vista alarm panel. This has an attribute beep attached to the sensor.<partition_name>_keypad that normally has a value of off but changes to beep 1 time or beep 3 times for a few seconds when the panel makes noise, such as when arming or disarming, or toggling the chime. I have a button item on my UI that is a custom template switch that I use for toggling the chime. It shows the mdi:bell icon when the chime is on and the mdi:bell-off icon when the chime is disabled. I thought it would be kind of neat if it showed the mdi:bell-ring icon when the beep attribute was active, before reverting back to the usual symbology. This worked, but the beep X times message stayed active for about 5 seconds, which was a little too long for my taste. I wanted it to last 1-2 seconds max before reverting back.

I first tried some template logic in my custom chime switch comparing now() or sensor_time_date_iso to the last_changed value of the beep attribute (which necessitated creating a template binary_sensor to convert the beep attribute to a state). This didn’t work well, I think because now() and sensor.time aren’t continuously updated.

Then I thought maybe I could use an automation triggered by the change in the beep attribute (now exposed as a sensor) to “re-set” the beep sensor back to off after 1-2 seconds, rather than waiting for it to revert on its own. That’s where this script really helped out. It doesn’t matter that it will be overwritten by the source sensor’s attribute, because the value that’s replacing it (off) is the same one I’m manually inserting a few seconds before.

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.