Trigger an insert on InfluxDB at a given time

Tags: #<Tag:0x00007f739a047260>

Hi.

I want to insert a value from HA into InfluxDB at a given time (once a day). Is that possible using an automation?

Sure. Set the sensor’s update interval to a very long time (weeks to months, you will restart before then) and use the update entity service to update the sensor when you want.

If the sensor is included (or not excluded) from the influxDB config the update will be written to the database.

Sorry, I did not get that.

Sensor value is reset every day at midnight, so I want to store its value before that happens (ie: at 23:55 or 11:55pm).

The sensor value will store in the db every time it changes so it will already have the last value before midnight.

Oh, I see…

The thing is, I don’t want to insert all values, only one “row” per day per sensor. I know I can group values by day in InfluxDB, but having only one record is enough and it is easier to query. What do you think?

Then go with this solution.

Set your scan interval to a very large number so it basically never updates:

Then use this automation to update it just before midnight:

  - alias: 'sensor_update'
    trigger:
      - platform: time
        at: '23:59:50'
    action:
      - service: homeassistant.update_entity
        entity_id: sensor.your_sensor_here

But if I do that, how would I see it updated in the UI? The sensor is updated regularly to show how many hours and minutes a heating panel was on during the day, like so:

In the card above, you can also see how many $ I spent on a given day.

Those two values are the ones I want to store at the end of the day (cost and time on).

Thanks for your help!

So it sounds like you want HA to basically work as normal, updating states as normal. But you want the state written out to Influx only once per day. Unfortunately that’s not really how the Influx integration works, it writes out all state changes to the entities you track as soon as they happen. Generally people prefer to use influx as their history storage and aggressively purge the HA database to keep its size down and performance high.

But if you want to do what you’re talking about the only thing I can think of is to create a time_throttle filter sensor for each entity you want to track and then tell Influx to exclude all entities besides those. You can set the time throttle sensors to make it so they only update for state changes once every x hours/minutes/seconds. This would probably be pretty close to what you’re looking for.

1 Like

For the record, Influx has a lot of querying capabilities. Even if you streamed out all your data to influx you should be able to construct whatever queries you want on that data.

For your particular use case, you should check out aggregateWindow. It filters the resultset to by aggregating the collected points using the function of your choice over the time window you specify. So for example a query like this:

from(bucket: "Home Assistant")
  |> range(start: -1y, stop: now())
  |> filter(fn: (r) => r._field == "value")
  |> aggregateWindow(every: 24h, fn: mean)

Will return me one value per day for the past year for each numeric sensor I store in Influx. That value will be the mean of all values collected for that sensor that day but you can pick any aggregator function you want.

This is what I ended up with

1 Like