Creating a binary sensor that gets its state set by a device with a time of state change passed as a parameter

Hello,

I am trying to create a simple binary sensor that gets its state set by a device that monitors the origin of the power source on the grid (generator/regular power).
The time of state change is recorded by the device and is not necessarily the same as the time home assistant receives a state change (i.e. network delays or temporarily down).
I have been struggling with the documentation and cannot figure out the most straightforward way to get this done.
This is a summary of my current understanding:

the most appropriate sensor type seems to be binary sensor.

there are many examples showing how to create a binary sensor however none of them mentions the entities that belong to that sensor. I am currently under the impression that the state of the sensor should be an entity and that entity should have an id. how to set those up? (given that this sensor is unique to this home assistant instance I do not need to get into creating several instances of that same sensor)

A rest sensor could get its state set by calling the rest endpoint (though the documentation never states if the endpoint urls are on the home assistant server and should be called by remote actors (device, some other service, etc…) or if they are remote urls that are called by home assistant to fetch the sensor state periodically. I am currently under the impression that the endpoint is hosted on the home assistant server and should be called by the device (or whatever service is managing the device).

One of the main parts that I am missing is how to get the custom date to be used instead of the current date as a date of state change. My current impression is that calling the rest api asking it to set sensor state to on or off will automatically use the current date.

Another question i’d like to raise is how to handle repeats of the same info? if the device does not get a confirmation of reception of a state post (network issues) from the endpoint it will repeat the post with the same info which will result in two simultaneous state changes with the same state status which is problematic for later usage of this information which expects those state changes to be a sequence of alternating on and off.

I have looked into webhooks at some point during my seeking a way to achieve this initial goal, but they seem to be mainly intended to trigger automations. and while they can trigger a service which sets a state this seems like an unnecessary circumvolution (it might be the only way to set the custom state change date for all i know - but the same question would apply: how to set a customized change date using a service)

I have no issue adding an integration with some custom python to achieve this result but that track is also murky and unless i want to do exactly what the examples i have found do, I am having trouble figuring out which functions to implement, how to get them to listen for state changes through a url (be it webhook, rest api or any alternative), and how to declare and handle the additional parameters.

Any hints, pointers or complete answers will be greatly appreciated. Many thanks in advance.

How is this device currently integrated with Home Assistant?

If it’s not yet integrated, how is the device’s data accessible? Is it via REST, MQTT, or something else?

1 Like

This is not possible (outside changing the underlying record in the database). HA manages the timestamp of when the state changed, and does not provide direct user control over it. What is it you’re wanting to do with the data?

1 Like

How is this device currently integrated with Home Assistant?

If it’s not yet integrated, how is the device’s data accessible? Is it via REST, MQTT, or something else?
(sorry new to this forum i don’t know how to properly quote previous messages)

This is all undecided at this stage (meaning open to all options). the easiest way i find to get the device talking to home assistant will be the one i go for (i can have the device implement any communication protocol as i can alter its code - the device could also talk to a web server and then this server can talk to home assistant). I felt like MQTT was not the best way to go as my limited knowledge of it leads me to believe it could loose some notifications so it will probably be something custom. it can make http calls and also receive http calls (not my preferred options as the periodical polling for updates seems like a waste compared to only pushing updates when they occur)

This is not possible (outside changing the underlying record in the database). HA manages the timestamp of when the state changed, and does not provide direct user control over it. What is it you’re wanting to do with the data?

Thank you for your reply. Is it also impossible if i make a new integration with its own python code that handles updates? i don’t have a clear picture of the possibilities in python - in the examples i have seen state changes happen using functions that do not take many parameters (binary sensor for example is a simple boolean). I can’t help but feel there has to be a way to achieve this as some devices record events that are might benefit form being sent with a timestamp of occurrence. though my current experience with power meters and home assistant is that any downtime leads to lost data.

Regarding your question about what i want to do with the data, I would like to trigger automations based on whether the power source is a generator or regular electricity (switches on and off), and filter a power meter readings to determine an approximation of consumption while on generator and while regular power grid.

The direct database access/tampering that you have suggested is my plan B. i could declare the sensor in home assistant configuration and then feed events (assuming i figure the hash thing correctly) and state changes from a completely separate software but i don’t like this option and would prefer to do things the home assistant way.

Even if you write your own code that uses the HA API, it will not be able to set the timestamp. If this were possible, HA would need to deal with this for interdependencies. For example, it would unnecessarily complicate automations if they had to deal with state changes arriving in the past.

However, you would be able to create your own integration that stored all the data required (ie. power source and date/time) and allowed HA to also store the timestamp (it’s also worth knowing that data for time ‘x’ actually came in at time ‘y’). Your integration could then use this data how it saw fit by getting the relevant state change history, to do the consumption calculation.

If you’re lucky, somebody else may have written such an integration, although my google skills were not up to finding one.

Many thanks Michael for taking the time to read this and try to figure out a fix. I have been trying to google this as well with no luck (this post came after a few days of trying to figure it out without asking). What you’re suggesting seems like a good alternative (if i understood it correctly), replace the binary sensor with whichever other type is best best suited to hold the time in the form of a date entity.

Edit: just found those two posts that address the issue of adding state changes at specific times instead of now:
https://community.home-assistant.io/t/import-old-energy-readings-for-use-in-energy-dashboard/341406
https://community.home-assistant.io/t/improved-support-for-long-term-historic-data/379659