Niggling problem with trigger sensor and last_updated

After some very helpful assistance with my previous problem, I now have trigger sensors working to record maximum power across various devices. I am now trying to take it one step further with our solar and have a notification if we hit a generation record for the day. I came up with the following which looks good, except that I have discovered that the last_updated attribute is applied if HA is restarted. So, where I was relying on this to determine whether a change has occurred during the day, the system believes it has if the system has been restarted.

alias: Announce new solar generation record
  description: Announce to the family that we have hit a new solar energy peak
  trigger:
  - platform: sun
    event: sunset
    offset: 0
  condition:
  - condition: template
    value_template: '{{ (as_timestamp(now())-as_timestamp(states.sensor.solar_peak_daily_energy.last_updated))
      < 43200}}'
  action:
  - service: notify.telegram_family
    data:
      message: We have hit a new solar daily energy record of {{ states('sensor.solar_peak_daily_energy') }} kWh!
      title: Announcement
  mode: single
  max_exceeded: silent

Instead of realying on the last_updated attribute, use an automation to store the time peak is reached to a datetime helper since they survive restart.

Thanks. I’ll have a go at working that out.

Thank you for your advice

Does this look to be heading in the right direction for the creation of the value to the helper?

alias: Save date of solar peak energy
description: Save date when solar peak energy is exceeded
trigger:
  - platform: state
    entity_id:
      - sensor.solar_peak_daily_energy
condition: []
action:
  - service: input_datetime.set_datetime
    data:
      date: "{{ now().timestamp() }}"
    target:
      entity_id: input_datetime.solar_energy_generation_peak
mode: single

You need to make one change to the action… if you are using a timestamp to update the helper your data key should be timestamp: not date:

alias: Save date of solar peak energy
description: Save date when solar peak energy is exceeded
trigger:
  - platform: state
    entity_id:
      - sensor.solar_peak_daily_energy
condition: []
action:
  - service: input_datetime.set_datetime
    data:
      timestamp: "{{ now().timestamp() }}"
    target:
      entity_id: input_datetime.solar_energy_generation_peak
mode: single

Thank you so much. This does seem incredibly arcane. I must confess that I don’t understand the distinction, but I assume that the “timestamp” version will work? Is one better than the other? Given that I only want to store the date and not the time?

Yes, using the timestamp: key will work with date-only, time-only, or date-and-time helpers.

No, using either option properly will have the same results. You just have to give the program the value in the format that you told it the information would be in…

FWIW, for date: it would need to look like:

action:
  - service: input_datetime.set_datetime
    target:
      entity_id: input_datetime.solar_energy_generation_peak
    data:
      date: "{{ now().strftime('%Y-%m-%d') }}"

Thank you very much.

I assume, then that I just change the original template condition now to

{{ (as_timestamp(now())-as_timestamp(input_datetime.solar_energy_generation_peak)) < 43200}}

To check that the stored value is within 12 hours of the sunset when I’m checking?

You’re missing a few things in your template to make it work:

{{ (as_timestamp(now()) - 
as_timestamp(states('input_datetime.solar_energy_generation_peak')) < 43200 }}

This is purely personal preference, but I like this method:

{{ states('input_datetime.solar_energy_generation_peak') 
== today_at().date() | string }}

Yes, your solution is much easier to read and closer to what I was aiming for. I was rather hampered by my “not knowing what I’m doing” level of experience.

Thank you once again