Increase sensor by 1 daily

I’m trying to create a sensor that ups it’s value with one every day.
My current config won’t show up in the developer options nor entities.

sensors:
  - trigger:
      - platform: time
        hours: 0
        minutes: 0 
        seconds: 0
    sensor:  
      - name: Levering elektra
        unique_id: levering_elektra
        unit_of_measurement: 'kWh'
        state: "{{ states('sensor.levering_elektra') | int + 1 }}"

The use of this sensor is to add a consumer to the Energy Dashboard with the daily usage fee (based on my monthly fee).

It’s probably better to use a input number which will remember it’s value past a restart.

Template sensors are restored.

Change the trigger to:

template:
  - trigger:
      - platform: time
        at: "00:00:00"
    sensor:  
      - name: Levering elektra
        unique_id: levering_elektra
        unit_of_measurement: 'kWh'
        state: "{{ states('sensor.levering_elektra') | int + 1 }}"

You seem to have used a miss-mash of time and time_pattern triggers. Also it goes under the template integration. Not “sensors”.

If it resets monthly you could just use the day number.

template:
  - sensor:
      - name: Levering elektra
        unique_id: levering_elektra
        unit_of_measurement: 'kWh'
        state: "{{ now().day }}"
1 Like

You’re correct, I’ve used time_pattern for testing, but the sensor isn’t showing up in HA.
I’ve created a template.yaml and made a referal to that file in my configuration.yaml (template: !include includes/template.yaml), my template.yaml starts with sensor: (the other sensors in template.yaml work).

For testing I’ve used the following and I hoped it would increment the value of sensor.levering_elektra with one every 10 seconds.

  - trigger:
      - platform: time_pattern
        hours: 
        minutes: 
        seconds: /10
    sensor:  
      - name: Levering elektra
        unique_id: levering_elektra
        unit_of_measurement: 'kWh'
        state: "{{ states('sensor.levering_elektra') | int + 1 }}"

Initially your sensor is undefined so the int filter does not work (there are probably errors about this in your logs). Supplying a default of 0 will fix this.

template:
  - trigger:
      - platform: time_pattern
        hours: 
        minutes: 
        seconds: /10
    sensor:  
      - name: Levering elektra
        unique_id: levering_elektra
        unit_of_measurement: 'kWh'
        state: "{{ states('sensor.levering_elektra') | int(0) + 1 }}"
1 Like

This was helpful to a problem I’m trying to solve; however, when I reload my configuration/reboot this reinitializes the sensor to 0 every time.

Is there a way to resolve this?

Probably better to increment a counter with an automation then.

I wanted to add a fixed daily value for grid consumption and found this solution helpful.
For resetting to 0 I just check for value and set accordingly.

  - trigger:
    - platform: time
      at: "23:59:00"
    sensor:  
    - name: "Grid Energy"
      unique_id: my_grid_energy
      unit_of_measurement: 'kWh'
      device_class: energy
      state_class: total_increasing
      state: >
        {% if has_value('sensor.grid_energy') %}
          {{ states('sensor.grid_energy') | int(0) + 9 }}
        {% else %}
          {{ 9 }}
        {% endif %}

There’s also this which may be a better solution: Powercalc - Virtual power sensors has virtual energy sensors too.

Thanks Tom for the link. I have data on my off-grid solar and only some household grid appliances going into HA. I also have energy data for the past 6 years and grid consumption is fairly consistent. Just wanted a simple means to reflect this unmeasured usage in the Energy tab.

My post was really on a solution to the initial sensor value if someone else happened upon this thread. I’ve only used it for a day but data is being incremented and saved and it survives a reboot.