How to write custom component sensor to update based on time of day

I am trying to write my first custom component for home assistant. I am trying to write a simulated sensor, similar to the simulated platform that exists. However with my simulated sensor, I am looking to have it change state based on the time of day. So as an example. Id like to write a simulated occupancy sensor where it triggers around a particular time of day. Part of the configuration may be the target trigger time and a random variance of say +/- 1 hour. The challenge I have in looking at the code for the existing simulated platform is how to get an update to trigger at a particular time of day as per the configuration of this platform.

I realize there is a sync and async method of updating these sensors and it seems it is encouraged to use the async method for efficiency and performance.

Does anyone have any ideas on how I might accomplish this? I am very new to HA development.

I also thought of just using the custom component to create the simulated sensors and use a python script to actually update the state of the sensors based on the system time.

Im sorry if what im asking for help with is not clear. I can certainly clarify further. I am basically trying to create a series of sensors that simulate real data ongoing with some sort of controlled randomness.

Thanks all

First of all I’m not an expert at the HA internals but I have written custom components and used the backend api. What about one of these

homeassistant.helpers.event.async_track_point_in_time(hass: homeassistant.core.HomeAssistant, action: Callable[[...], None], point_in_time: datetime.datetime) → Callable[[], None][source]

    Add a listener that fires once after a specific point in time.

homeassistant.helpers.event.async_track_point_in_utc_time(hass: homeassistant.core.HomeAssistant, action: Callable[[...], Any], point_in_time: datetime.datetime) → Callable[[], None][source]

    Add a listener that fires once after a specific point in UTC time.

You could calculate the configured time with a random offset and then add a listener at that time to get a callback. You then set it again for the next time. Looks like you pass in hass, your callback function and the datetime value. I’ve never used it myself but this might be what you’re looking for :wink:

Hi there @davesmeghead. I want to thank you for taking the time to respond. I think this is exactly what im looking for. I will take a run with this and see how far I get. There is a lot of learning on my part but this will give me a good jump start.

Thank you again… ill report back when I get something going.

All the best.

Hi there @davesmeghead, just wanted to follow up. I was able to get it working using the method you suggested. Here’s the code that I added to update an event. I added event handler first in the of my integration:

 def handle_event(self):

Then I added a call to register a listener for a particular time. I added this call:

        hass, handle_event, dt_util.as_utc(datetime.datetime(2020, 8, 26, 14, 27))

So this registers a listener for that particular time (UTC) and then calls the handle_event at that time. The only thing with this is that when I restart home assistant, if the time of my listener is before the actual time, then the event just fires. I would have expected it to not fire until that specific time is hit again, but in reading the docs on the function call, this is working as expected.

Thanks again for pointing me in the right direction.


1 Like