Yesterday powerusage is somehow mixed with todays?

I have set up some powermonitoring.

For this I have created an input_number called

input_number.yesterday_powerusage

And an accompanying sensor (to make it readonly in the UI):

  - template:
      total_daily_power_yesterday:
        friendly_name: Gårsdagens strømforbrug
        unit_of_measurement: kWh
        value_template: "{{ states('input_number.yesterday_powerusage') | float }}"
        device_class: power

I also have a sensor that accumulates all power for today’s ‘daily totals’ that are built into the devices:


    total_daily_power:
        friendly_name: Samlet forbrug for i dag
        unit_of_measurement: kWh
        value_template: "{{ states.sensor|selectattr( 'entity_id','in',state_attr('group.daily_power_usage','entity_id')) | rejectattr('entity_id', 'equalto', 'sensor.power') | map(attribute='state')|map('float')|sum|round(3) }}"
        device_class: power

I’ve put all the ‘daily sensors’ into a group to have them summed up correctly.

Once a day (at 23:58) I have an automation that copies the ‘daily power’ to ‘yesterdays power’.

My problem is that my ‘todays total power’ just shows the same number as the ‘yesterdays power’.
So it just shows what value it had yesterday at 23.58, it doesn’t show what has been used today up until now.

What if you force an update?

1 Like

You will have to list all your sensors in your template sensor. The extract_entities function used by home assistant to determine what to monitor in a template will not work with the template you have. See more here:

1 Like

What tom_I said; your Template Sensor doesn’t work the way you think it does.

How many identifiable entities do you see in this template?

{{ states.sensor
  | selectattr('entity_id','in',state_attr('group.daily_power_usage','entity_id'))
  | rejectattr('entity_id', 'equalto', 'sensor.power')
  | map(attribute='state')
  | map('float') | sum | round(3) }}

There are two:

  1. group.daily_power_usage
  2. sensor.power

On startup, Home Assistant will assign listeners for those two entities. Whenever they change state, the template is evaluated. So if you expected the template to be evaluated whenever one of the members of the group changes its state or if any sensor changes state (via states.sensor), that’s not happening.

There’s a major re-engineering underway that will greatly improve how entities are identified within a Template Sensor’s template (and Template Switch and Binary Sensor and etc). There was an initial improvement introduced in 0.110 (to handle the expanding of groups) but it didn’t work. There was work performed to correct it in 0.114 but that also didn’t work. This new initiative is completely different and will do more than just support the expansion of groups (it will also be able to identify states.sensor).

For now, add an entity that updates regularly, like sensor.time, to force the template to be evaluated periodically (every minute if you use sensor.time and at the beginning of each day if you use sensor.date). Ensure sensor.time is defined before you use it in the template.

    total_daily_power:
        friendly_name: Samlet forbrug for i dag
        unit_of_measurement: kWh
        entity_id: sensor.time
        value_template: >
          {{ expand('group.daily_power_usage')
            | rejectattr('entity_id', 'eq', 'sensor.power')
            | map(attribute='state')
            | map('float') | sum | round(3) }}
        device_class: power

Note

When you add entities to entity_id: in a Template Sensor, listeners will be assigned to those entities only and not to any identifiable entities in the template. For the example I posted, it means a listener will be created for sensor.time only and not for group.daily_power_usage or sensor.power.

1 Like

WOW @123 That was a very thorough explanation, thankyou very much for the time you took to look into this.
It makes complete sense for me, I wasn’t aware of the evaluation process.

I’ll go and change the setup to your suggestion and see what happens.

1 Like

It’s working as far as I can see, that is very nice!