-
-
Notifications
You must be signed in to change notification settings - Fork 28.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
persistent trigger based sensors #49698
Comments
Hey there @PhracturedBlue, @tetienne, mind taking a look at this issue as its been labeled with an integration ( |
Whether it's a Template Sensor or the recently introduced Trigger-based Template Sensor, its value is not "persistent" nor does it "survive a restart". In the case of Template Sensors, their template is always evaluated on startup (and is not stored/restored like Helpers). To make a Trigger-based Template Sensor behave the same way, just add another trigger: - trigger:
- platform: state
entity_id: cover.living_room_shutters
- platform: homeassistant
event: start Now it will also be evaluated on startup. |
Ok, but what's the point? There is no data in that 'start' trigger. Look at the code - sensor information was extracted from the trigger, from changing the value of something. Start event has no interesting data in it. Your solution is good only for time based trigger sensors. This is not the case. |
You have a valid point; your template cannot be redesigned to avoid using the It would appear that a Trigger-based Template Sensor is not the right choice for your application. One possible workaround is to convert your Trigger-based Template to an automation that writes its computed value to a Helper like an If a Helper isn't how you want the value to be presented, another possibility is to make the automation publish its computed value to an MQTT topic (as a retained message). Create an MQTT Sensor that subscribes to the topic. Now you have a sensor (that can have device_class, unit_of_measurement, etc) with a persistent value (because the value is stored by the MQTT broker) that is instantly restored on startup (i.e. the moment Home Assistant reconnects to the broker). |
This conclusion is very wrong. Look at trigger based documentation:
This is exactly my case: I need access to trigger data during sensor value update. I understand that triggered based sensors are exactly to not use those input helpers which can be manually altered and is very awkward solution. |
yes, I've already mentioned this solution at the top. I have such sensors based on events coming from mobile app. I've done it like that because there was no other way to quit input helpers. And since this is sensor data and not user input, I don't want to use helpers. But now we have trigger base sensors and I think this is great solution! Well, until you realize this sensor is very ephemeral. Is this a bug or what? |
I did and the excerpt you selected explains precisely how it currently works but doesn't refer to the added feature you want which is for the sensor's value to somehow "persist" in the absence of a trigger. The fact is, on startup, your example will have no value until it is triggered. The computed value will persist in Home Assistant's state machine until a restart (just like all other sensors).
It's not a bug. What you want is simply not the way it works (or was ever intended to). What you want constitutes a Feature Request and would probably have to go through an Architectural review because it requests behavior that differs from all other sensors (no sensor values are stored by Home Assistant; on startup all are either retrieved from an integration or computed by templates). |
Following, since I have same kind of issue here, I want to trigger an sensor update once a day.
Honestly, it sounds quite hard to have something that would fit every use cases EDIT: |
Did you manage to make it work for template_reload?
And I get the following error: |
Was thinking about this lately again. Maybe the issue actually comes down to exactly this: getting startup value from something. So having this kind of attribute I could decide to get startup value from history (per SQL query?) or maybe mqtt topic? Recorder history looks quite good and should not require any architectural changes, right? |
I deleted two of my posts because what I wrote was wrong and didn't want them to mislead anyone. thomasgermain provided the correct solution. Simply add the following trigger to your Trigger-based Template Sensor. - platform: event
event_type: event_template_reloaded It will cause the sensor to compute its |
But there is no information in this event trigger. It doesn't even have state object
|
You're right; in its current form, the Trigger-based Template Sensor is inappropriate for your application. You'll need to do use another method. |
There is a precedence for persistent sensors and that is utility meter |
As mentioned previously, Issues are for bug reports, not Feature Requests. Discussing it here won't change the status quo. You may have noticed that since this Issue was opened, now 24 days ago, no one from the development team has been assigned to it or has commented. |
ok guys, @PhracturedBlue , @tetienne , looking at documentation |
There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. |
latest updates don't do any good with this. Still there is no way to recover template sensor last value after ha restart. |
There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. |
The problem
How to make trigger based sensors value persistent and survive restart? Trigger based sensors always have state unknown until next trigger.
This sensor defined below was not directly possible without trigger based sensors because it needs access to previous and current state to calculate the difference.
It's always an option to push event to mqtt and make sensor out of that but there should be other way, right?
What is version of Home Assistant Core has the issue?
core-2021.4.6
What was the last working version of Home Assistant Core?
No response
What type of installation are you running?
Home Assistant OS
Integration causing the issue
No response
Link to integration documentation on our website
https://www.home-assistant.io/integrations/template/#configuration-for-trigger-based-template-sensors
Example YAML snippet
Anything in the logs that might be useful for us?
No response
Additional information
No response
The text was updated successfully, but these errors were encountered: