Daily min max temperature with time stamp

You can’t format a datetime helper.
But you can use a input_text and it should work

Ok did not know that. Thanks. Will look into that

It worked. Thanks for the help. Another challenge solved.

1 Like

Would really love to see the code behind your setup on max/min temperature with timestamp. This is exactly what I looking for :heart_eyes:

1 Like

Hi.
I’ve managed this through an automation and some numeric and text helpers. The automation is resetting the helpers, the daily will be reset daily at 00:00:01 the weekly on sundays monthly on the 1st etc. You need to create text inputs helpers for the date time stamps i.e. ‘daily min temp’ or whatever you want to call it. And numeric helpers to store the temp. At 00:00:01 the temp is set for max to -99.0. Every time the temp of the sensor changes and it is higher then the stored value it overwrites the helper value. Same for min but then the stored start value is 99.0. For displaying I use a combination of horizontal, vertical, and the tile card on the dashboard. Below the codes for resetting the helpers, storing the min max and time values and displaying on the dashboard. Let me know if you need more help.

Here is the automation code to reset the helpers.

description: ""
trigger:
  - platform: time
    at: "00:00:01"
condition: []
action:
  - choose:
      - conditions: []
        sequence:
          - service: input_number.set_value
            data:
              value: 99
            target:
              entity_id: input_number.min_tuin_temp
          - service: input_number.set_value
            data:
              value: -99
            target:
              entity_id: input_number.max_tuin_temp
    alias: Dagelijkse reset
  - choose:
      - conditions:
          - condition: time
            after: "00:00:00"
            weekday:
              - mon
            before: "00:00:00"
        sequence:
          - service: input_number.set_value
            data:
              value: 99
            target:
              entity_id:
                - input_number.min_tuin_temp_weekly
          - service: input_number.set_value
            data:
              value: -99
            target:
              entity_id:
                - input_number.max_tuin_temp_weekly
    alias: Weekelijkse reset
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ now().day == 1 }}"
        sequence:
          - service: input_number.set_value
            data:
              value: -99
            target:
              entity_id: input_number.max_tuin_temp_monthly
          - service: input_number.set_value
            data:
              value: 99
            target:
              entity_id: input_number.min_tuin_temp_monthly
    alias: Maandelijkse reset
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ now().day == 1 and now().month == 1}}"
        sequence:
          - service: input_number.set_value
            data:
              value: -99
            target:
              entity_id: input_number.max_tuin_temp_yearly
          - service: input_number.set_value
            data:
              value: 99
            target:
              entity_id: input_number.min_tuin_temp_yearly
    alias: Jaarlijkse reset
  - service: automation.trigger
    data: {}
    target:
      entity_id: automation.dagelijkse_min_tuin_temp
mode: single

And here is the code for setting the date and temp values


alias: Set min max tuin temperaturen
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.cresta_5402_temp
condition: []
action:
  - choose:
      - conditions:
          - condition: numeric_state
            entity_id: sensor.cresta_5402_temp
            below: input_number.min_tuin_temp
        sequence:
          - service: input_number.set_value
            data:
              value: "{{ states('sensor.cresta_5402_temp') | float }}"
            target:
              entity_id: input_number.min_tuin_temp
          - service: input_text.set_value
            data:
              value: "{{ now().strftime('%H:%M %d-%m-%y') }}"
            target:
              entity_id: input_text.min_tuin_temp_tijd
    alias: Set min temp daily
  - choose:
      - conditions:
          - condition: numeric_state
            entity_id: sensor.cresta_5402_temp
            above: input_number.max_tuin_temp
        sequence:
          - service: input_number.set_value
            data:
              value: "{{ states('sensor.cresta_5402_temp') | float }}"
            target:
              entity_id: input_number.max_tuin_temp
          - service: input_text.set_value
            data:
              value: "{{ now().strftime('%H:%M %d-%m-%y') }}"
            target:
              entity_id: input_text.max_tuin_temp_tijd
    alias: Set max temp daily
  - choose:
      - conditions:
          - condition: numeric_state
            entity_id: sensor.cresta_5402_temp
            below: input_number.min_tuin_temp_weekly
        sequence:
          - service: input_number.set_value
            data:
              value: "{{ states('sensor.cresta_5402_temp') | float }}"
            target:
              entity_id: input_number.min_tuin_temp_weekly
          - service: input_text.set_value
            data:
              value: "{{ now().strftime('%H:%M %d-%m-%y') }}"
            target:
              entity_id: input_text.min_tuin_temp_tijd_weekly
    alias: Set Min tuin temp weekly
  - choose:
      - conditions:
          - condition: numeric_state
            entity_id: sensor.cresta_5402_temp
            above: input_number.max_tuin_temp_weekly
        sequence:
          - service: input_number.set_value
            data:
              value: "{{ states('sensor.cresta_5402_temp') | float }}"
            target:
              entity_id: input_number.max_tuin_temp_weekly
          - service: input_text.set_value
            data:
              value: "{{ now().strftime('%H:%M %d-%m-%y') }}"
            target:
              entity_id: input_text.max_tuin_temp_tijd_weekly
    alias: Set Max tuin temp weekly
  - choose:
      - conditions:
          - condition: numeric_state
            entity_id: sensor.cresta_5402_temp
            below: input_number.min_tuin_temp_monthly
        sequence:
          - service: input_number.set_value
            data:
              value: "{{ states('sensor.cresta_5402_temp') | float }}"
            target:
              entity_id: input_number.min_tuin_temp_monthly
          - service: input_text.set_value
            data:
              value: "{{ now().strftime('%H:%M %d-%m-%y') }}"
            target:
              entity_id: input_text.min_tuin_temp_tijd_monthly
    alias: Set Min tuin temp monthly
  - choose:
      - conditions:
          - condition: numeric_state
            entity_id: sensor.cresta_5402_temp
            above: input_number.max_tuin_temp_monthly
        sequence:
          - service: input_number.set_value
            data:
              value: "{{ states('sensor.cresta_5402_temp') | float }}"
            target:
              entity_id: input_number.max_tuin_temp_monthly
          - service: input_text.set_value
            data:
              value: "{{ now().strftime('%H:%M %d-%m-%y') }}"
            target:
              entity_id: input_text.max_tuin_temp_tijd_monthly
    alias: Set Max tuin temp monthly
  - choose:
      - conditions:
          - condition: numeric_state
            entity_id: sensor.cresta_5402_temp
            below: input_number.min_tuin_temp_yearly
        sequence:
          - service: input_number.set_value
            data:
              value: "{{ states('sensor.cresta_5402_temp') | float }}"
            target:
              entity_id: input_number.min_tuin_temp_yearly
          - service: input_text.set_value
            data:
              value: "{{ now().strftime('%H:%M %d-%m-%y') }}"
            target:
              entity_id: input_text.min_tuin_temp_tijd_yearly
    alias: Set Min tuin temp yearly
  - choose:
      - conditions:
          - condition: numeric_state
            entity_id: sensor.cresta_5402_temp
            above: input_number.max_tuin_temp_yearly
        sequence:
          - service: input_number.set_value
            data:
              value: "{{ states('sensor.cresta_5402_temp') | float }}"
            target:
              entity_id: input_number.max_tuin_temp_yearly
          - service: input_text.set_value
            data:
              value: "{{ now().strftime('%H:%M %d-%m-%y') }}"
            target:
              entity_id: input_text.max_tuin_temp_tijd_yearly
    alias: Set Max tuin temp yearly
mode: single

Code for the dashboard


type: vertical-stack
cards:
  - type: horizontal-stack
    cards:
      - type: horizontal-stack
        cards:
          - type: tile
            name: Max vandaag
            entity: input_number.max_tuin_temp
            color: light-green
      - type: tile
        name: Tijdstip
        entity: input_text.max_tuin_temp_tijd
        color: light-green
  - type: horizontal-stack
    cards:
      - type: tile
        entity: input_number.min_tuin_temp
        name: Min vandaag
        color: pink
      - type: tile
        name: Tijdstip
        entity: input_text.min_tuin_temp_tijd
        color: pink
  - type: horizontal-stack
    cards:
      - type: tile
        entity: input_number.max_tuin_temp_weekly
        name: Max week
        color: light-green
      - type: tile
        name: Tijdstip
        entity: input_text.max_tuin_temp_tijd_weekly
        color: light-green
  - type: horizontal-stack
    cards:
      - type: tile
        entity: input_number.min_tuin_temp_weekly
        name: Min week
        color: pink
      - type: tile
        name: Tijdstip
        entity: input_text.min_tuin_temp_tijd_weekly
        color: pink
  - type: horizontal-stack
    cards:
      - type: tile
        entity: input_number.max_tuin_temp_monthly
        name: Max maand
        color: light-green
      - type: tile
        name: Tijdstip
        entity: input_text.max_tuin_temp_tijd_monthly
        color: light-green
  - type: horizontal-stack
    cards:
      - type: tile
        entity: input_number.min_tuin_temp_monthly
        name: Min maand
        color: pink
      - type: tile
        name: Tijdstip
        entity: input_text.min_tuin_temp_tijd_monthly
        color: pink
  - type: horizontal-stack
    cards:
      - type: tile
        entity: input_number.max_tuin_temp_yearly
        name: Max jaar
        color: light-green
      - type: tile
        name: Tijdstip
        entity: input_text.max_tuin_temp_tijd_yearly
        color: light-green
  - type: horizontal-stack
    cards:
      - type: tile
        name: Min jaar
        entity: input_number.min_tuin_temp_yearly
        color: pink
      - type: tile
        name: Tijdstip
        entity: input_text.min_tuin_temp_tijd_yearly
        color: pink

4 Likes

I don’t like this combination of an automation setting a helper, so rather than this I would try a trigger based template sensor.

Something like this (not tested):

template:
  - trigger:
    - platform: time
      at: "00:00:00"
    - platform: template
      value_template: "{{ states('sensor.home_average_temperature') > states('sensor.home_todays_max_average_temperature') }}"
    sensor:
      - name: Home - Todays max average temperature
        unique_id: home_todays_max_average_temperature
        unit_of_measurement: "°C"
        device_class: temperature
        icon: mdi:thermometer-chevron-up
        state: "{{ states('sensor.home_average_temperature')}}"
        attributes:
          datetime: "{{ now() }}"

  - trigger:
    - platform: time
      at: "00:00:00"
    - platform: template
      value_template: "{{ states('sensor.home_average_temperature') < states('sensor.home_todays_min_average_temperature') }}"
    sensor:
      - name: Home - Todays min average temperature
        unique_id: home_todays_min_average_temperature
        unit_of_measurement: "°C"
        device_class: temperature
        icon: mdi:thermometer-chevron-down
        state: "{{ states('sensor.home_average_temperature')}}"
        attributes:
          datetime: "{{ now() }}"

I think it looks more elegant and it’s much easier to maintain. And it will be easy to replicate for annually/monthly/weekly/hourly sensors as you might want.
You can use the same triggers used on the automations suggested in this thread, if you wanna improve the one from my example.

Edit on 2023-01-12 18:41UTC: Replaced the template trigger used to detect a new day by a time patter to trigger at midnight. My intention is to reduce chance of errors and probably reduce CPU consumption a bit.

4 Likes

Hi,
Interesting option. Can you explain why you don’t like to use the automation. And where do I have to store this code?

You can have this in your configuration.yaml.

My point about automation is not in the automation itself, but the fact that I have to maintain an automation and a helper, and also I have to be the one remembering which automation controls each helper.
But the biggest point for me is tat what you are looking for is a sensor, then in a few months from now, when looking for this data you most likely will first search for a sensor.something before having to remember you have used a combination of aumation + helper and then start searching for input_something..
If what I want in the end is a sensor, let’s create a sensor, not an automation. I will leave automations to automate things (mostly actions) in my home.

2 Likes

Thanks a lot! Really appreciate! :grinning:

1 Like

By the way, I was reviewing my proposal and then I replaced the template trigger used to detect a new day by a time patter to trigger at midnight. My intention is to reduce chance of errors and probably reduce CPU consumption a bit.

1 Like

Ah that makes sense. Will start converting.

Works great for me. Thanks for sharing.

1 Like

I got this error while using the code of @EdwardTFN :

Logger: homeassistant.config
Source: config.py:868
First occurred: 22:26:51 (1 occurrences)
Last logged: 22:26:51
Invalid config for [template]: [trigger] is an invalid option for [template]. Check: template->sensor->2->trigger. (See /config/configuration.yaml, line 46). 

Did I miss something ?
Thank you,

Please share your yaml.

I’m not getting this to work either, though I am not getting any errors. It just tells me the sensor is unavailable.
Here’s the YAML from my configuration.yaml:

template:
  - trigger:
    - platform: time
      at: "00:00:00"
    - platform: template
      value_template: "{{ states('sensor.gw1100b_temp') > states('sensor.outdoor_temperature_max') }}"
    sensor:
      - name: "Outdoor Temperature Max"
        unique_id: temp_outdoor_max
        unit_of_measurement: "F"
        device_class: temperature
        icon: mdi:thermometer-chevron-up
        state: "{{ states('sensor.gw1100b_temp') }}"
        attributes:
          datetime: "{{ now() }}"

sensor.gw1100b_temp is the sensor, and it has a numeric value.
The value_template resolves to True in DevTools.
But the sensor isn’t getting a value assigned:
image
I was thinking it was an issue with the state line, but then I realized it also doesn’t have a datetime attribute. So I’m not sure. (And upon closer inspection, the unique_id doesn’t match the entity id; it’s getting that from the name).
Could it be an issue with the trigger seeing an AND clause for the time and template? (So it only sets it at midnight, and then never throughout the day, as opposed to an OR). I just added this today and haven’t let it sit overnight yet. If it sets a temp at midnight, that’ll confirm that theory.

Which version of Home Assistant are you using?
Could you please share your yaml?

I don’t see anything wrong with your code. I’ve left that code running since the day I’ve published here and it still working fine:


Any chance your temperature sensor is going “unknown” from time to time? Could you double check on its history?

Do you have any error into HA logs (Settings > System > Logs)?

In Home Assistant triggers are always “OR” and conditions are always “AND”.

Turns out (for whatever reason) it needs to get to that first trigger (midnight).
I checked this morning, and it started promptly working at midnight and has been correctly updating ever since.


(I have one for min as well, and it has been sitting at 69 since midnight, so it looks like that’s working well)

2 Likes

Thank you very much!
I don’t understand if with the instruction :" - platform: time at: “00:00:00” " the Min/Max value is automatically reset, or should the initialization of the field also be entered?

Were you able to make a trigger also for MIN/MAX of the month?
How you do it? Could you please send me the code to start the trigger on 1th of the month and also of the year?

Thank you!