Help - History Statistics Sensor from AppDaemom

Hi guys!

Does anyone have an example of how to use History Statistics Sensor or InfluxDB ?

I’m trying to write the following app:
Notify me if my electricity consumption is more than X kw in 3 weeks

thanks in advance,

I have an app pulls data from Influx to create a chart. You can execute any query against the database

class InfluxChart(hass.Hass):
    def initialize(self):
        self.log("Hello from InfluxChart Data Plot App") = self.args.get("host", "localhost")
        self.user = self.args.get("user", "home_assistant")
        self.password = self.args.get("password", None)
        self.dbname = self.args.get("dbname", "home_assistant")
        self.query = self.args.get("query", None)
        if not self.entity_exists("sensor.monthlypower"):
           self.log("Our virtual sensor doesn't exist so we'll create it now to prevent false alerts")    
           self.set_state("sensor.monthlypower", state = 0)
        #run at midnight
        self.run_daily(self.create_chart, datetime.time(23, 55, 50))
        self.listen_event(self.ha_event, "plugin_started")
        self.listen_event(self.appd_event, "appd_started")

    def ha_event(self, event_name, data, kwargs):
      self.log("Home Assistant restart detected")
      self.run_in(self.create_chart, 60)
    def appd_event(self, event_name, data, kwargs):
      self.log("AppDaemon restart detected")
      self.run_in(self.create_chart, 180)
  class: InfluxChart
  module: influx_chart
  host: localhost
  user: home_assistant
  password: 'xxx'
  dbname: home_assistant
  query: "select max(value) from kWh where entity_id = 'energy_generation' and time >= now() - 30d group by time(1d) fill(null) order by time asc;"

Hope this helps

1 Like

Many thanks @anilet :+1: