All Time High Sensor Date/Time

I have a sensor that shows me my all-time high power for my solar panels. I want to make something that shows the date+time when this record first ocurred, but don’t know how to go about it. I’ve tried several iterations, but it either seems to result in an unknown, or shows the time/date when the sensor was updated by home assistant, which isn’t helpful in this case.

My template for getting the record:

template:
  - trigger:
      - platform: state
        entity_id: sensor.givtcp_pv_power
        not_to:
          - unavailable
          - unknown
    sensor:
      - name: Highest Solar Ever
        unique_id: c6174f4d-0709-4e70-a675-5ba3f07c80a0
        state: >
          {{ max(trigger.to_state.state | float, this.state | float(-999)) }}

Thanks

I would approach this using an input_number helper to hold the record high data and use:
states.input_number.pv_max.last_changed
To retreave the date_time of the occurance. pv_max is just a place holder name it as needed.
EDIT: last_updated > last_changed my bobo

Thanks for your help. This isn’t something I’ve had to do before, so could you help me out with it please? Am I right in thinking I set the input number helper up as a kind of placeholder and then create an automation that updates that value? Also in your example, would I call the input number helper just pv_max, or the whole states.input_number.pv_max.last_changed?
Thanks again.

template:
  - trigger:
      - platform: state
        entity_id: sensor.givtcp_fd2319f222_pv_power
        not_to:
          - unavailable
          - unknown
    sensor:
      - name: Highest Solar Ever
        unique_id: c6174f4d-0709-4e70-a675-5ba3f07c80a0
        state: >
          {{ max(trigger.to_state.state | float, this.state | float(-999)) }}
        attributes:
          recorded: >
            {{ now().isoformat()
               if trigger.to_state.state | float > this.state | float(-999) 
               else this.attributes.recorded | default(now().isoformat()) }}

I’m still quite new to HA & I try to keep to simple solutions as I gain in knowledge so 123 Taras’ may be far more elegant than what is below.

HA equips the input_number helper with field last_changed. So:

if current pv_power > input_number.pv_max
  input_number.pv_max = pv_power
endif
^^^ Sudo code ^^^

The above captures the max value and the last_changed field will retain the datetime of the latest maximum. All in the ‘input_number.max_pv’ helper. HA should never forget these as well. That may be also true with the template sensor example above though I’ve not proven that as yet.
The template output below demonstrates how to access the last_changed field and format it as human readable. It is using a helper on my instance.

{{ states.input_number.arrive_delay_long.last_changed | as_local | as_timestamp | timestamp_custom('%D %-I:%M %p') }}

Output "03/25/24 10:13 PM"

I got the above template line from HA Documentation - Template Entities and last_changed/last_updated
Best of luck with your quest.
ADDED: Template output

Thanks for this. How will it work in practice? Will this record the record solar then keep the record+time and date separately? Then I just view sensor highest solar ever to view the time and date of the record?

Would this be a script or a template then? Apologies for the confusion

The Template Sensor’s state value reports the highest value of sensor.givtcp_fd2319f222_pv_power. Its attribute named recorded contains the date and time (in ISO format) when the highest value was detected.

That seems to work great. Thanks very much for all your help and explanations.

1 Like

Took me a while to realise you meant “pseudo-code” as sudo has a different meaning.

image

Sorry about that @Troon. Still pretty new to HA. What would be the correct nomenclature for “sudo code” ie mockup code, if any?

No apology needed. The term (as I linked above) is “pseudo-code”.

Touché my friend!

1 Like