Adding timestamp to maximum value of entity in a 24 hr period

I’ve successfully created a new custom sensor to return the maximum output value of my Solar PV system each day (this resets at midnight) using the script below, however, I would like to add the timestamp to the output entity i.e. hour, minutes and seconds in a 24hr format. How would I do this?

New Sensor setup Max solar house roof
  • trigger:
    • platform: time
      at: ‘00:00:00’
    • platform: state
      entity_id: sensor.3_house_roof_solar_power
      sensor:
    • name: ‘Solar House Daily Max’
      unique_id: ‘Solar_house_daily_max’
      unit_of_measurement: ‘W’
      device_class: power
      state: >
      {% set t_new = states(‘sensor.3_house_roof_solar_power’) | float(-50) %}
      {{ [t_new, this.state | float(-50)] | max if trigger.platform != ‘time’ else t_new }}

Thanks

Please follow Community Guideline #11: Format it Properly

Do you want the timestamp include in the state or as an attribute?

  - trigger:
      - platform: time
        at: '00:00:00'
      - platform: state
        entity_id: sensor.3_house_roof_solar_power
        not_to:
          - unknown
          - unavailable
    action:
      - variables:
          t_new: "{{ states('sensor.3_house_roof_solar_power') | float(-50) }}"
    sensor:
      - name: Solar House Daily Max
        unique_id: Solar_house_daily_max
        unit_of_measurement: 'W'
        device_class: power
        state: >
         {{ [t_new, this.state | float(-50)] | max if trigger.platform != 'time' else t_new }}
        attributes:
          time: >
            {{ now().strftime('%H:%M:%S') if trigger.platform == 'time'
              or t_new > this.state | float(-50) 
              else this.attributes.time|default('00:00:00') }}

Thanks for responding. Apologies, not sure why my original post was not formatted correctly. I’m looking for the timestamp to be included in the state, but it would be good to know how to show as a attribute too. .

If you do that then you will get an error message. Why? Because you have specified unit_of_measurement which means the state value must be numeric. Adding time as hh:mm:ss to the state value will make it non-numeric.

That’s why the example I posted puts the timestamp in an attribute, not in the state.

Thanks for the explanation