Positive and negative History Stats

HI

I need to measure the daily number of hours that a power sensor is above zero and the number of hours it is below zero.

I have set up the following sensors:

- platform: history_stats
  name: Import Hours Today
  entity_id: sensor.import
  state: 'true'
  type: time
  start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
  end: '{{ now() }}'

- platform: history_stats
  name: Export Hours Today
  entity_id: sensor.import
  state: 'false'
  type: time
  start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
  end: '{{ now() }}'

- platform: template
  sensors:
    import:
      value_template: "{{states.sensor.grid_power.state > '0' }}"

sensor.grid_power is the sensor that measures the power ie + or - (defined elsewhere)

sensor.import gives ‘true’ if +ve and ‘false’ if -ve

The 2 history_stats sensors remain at 0.0h

What am I missing

My approach would be to create a template binary sensor binary_sensor.import that is True when states.sensor.grid_power.state > 0. If I’m correct, you are using a regular template sensor. Perhaps changing this will solve the issue. If not, please let me know.

1 Like

Thank you for the input. You got me thinking.
The solution was to capitalise ‘true’ and ‘false’ in the history_stats sensors as the Import sensor returns ‘True’ or ‘False’ not ‘true’ or ‘false’
Now it works.

1 Like