Set min/max temperature from midnight

Hello

using python_scripts.set_state i write an automation that set a min/max temperature from midnight, but i don’t understand why the automation set both min and max sensor in a same time with the same value… FIXED

sensor:

- platform: template
  sensors:
    min_temp_outside:
      friendly_name: "Temperatura minima"
      value_template: "0"
      unit_of_measurement: °C
    min_temp_outside_time:
      friendly_name: "Ora temperatura minima"
      value_template: "0:00:00"
    max_temp_outside:
      friendly_name: "Temperatura massima"
      value_template: "0"
      unit_of_measurement: °C
    max_temp_outside_time:
      friendly_name: "Ora temperatura massima"
      value_template: "0:00:00"

automation:

alias: outside temp
description: ""
trigger:
  - platform: time
    at: "00:00:00"
    id: mezzanotte
  - platform: state
    entity_id:
      - sensor.balcone_temperatura
  - platform: homeassistant
    event: start
    id: boot
condition: []
action:
  - if:
      - condition: or
        conditions:
          - condition: trigger
            id: mezzanotte
          - condition: trigger
            id: boot
    then:
      - service: python_script.set_state
        data_template:
          entity_id: sensor.max_temp_outside
          state: "{{ states.sensor.balcone_temperatura.state }}"
      - service: python_script.set_state
        data_template:
          entity_id: sensor.max_temp_outside_time
          state: >-
            {% set t = now() %} {% set giorno = strptime("%0.04d-%0.02d-%0.02d
            %0.02d:%0.02d:%0.02d" | format(t.year, t.month, t.day, t.hour,
            t.minute, t.second), "%Y-%m-%d %H:%M:%S") %} {{
            giorno.strftime("%H:%M:%S") }}
      - service: python_script.set_state
        data_template:
          entity_id: sensor.min_temp_outside
          state: "{{ states.sensor.balcone_temperatura.state }}"
      - service: python_script.set_state
        data_template:
          entity_id: sensor.min_temp_outside_time
          state: >-
            {% set t = now() %} {% set giorno = strptime("%0.04d-%0.02d-%0.02d
            %0.02d:%0.02d:%0.02d" | format(t.year, t.month, t.day, t.hour,
            t.minute, t.second), "%Y-%m-%d %H:%M:%S") %} {{
            giorno.strftime("%H:%M:%S") }}
    else:
      - if:
          - condition: template
            value_template: >-
              {{ states.sensor.balcone_temperatura.state | float >
              states.sensor.max_temp_outside.state | float }}
        then:
          - service: python_script.set_state
            data_template:
              entity_id: sensor.max_temp_outside
              state: "{{ states.sensor.balcone_temperatura.state }}"
          - service: python_script.set_state
            data_template:
              entity_id: sensor.max_temp_outside_time
              state: >-
                {% set t = now() %} {% set giorno =
                strptime("%0.04d-%0.02d-%0.02d %0.02d:%0.02d:%0.02d" |
                format(t.year, t.month, t.day, t.hour, t.minute, t.second),
                "%Y-%m-%d %H:%M:%S") %} {{ giorno.strftime("%H:%M:%S") }}
      - if:
          - condition: template
            value_template: >-
              {{ states.sensor.balcone_temperatura.state | float <
              states.sensor.min_temp_outside.state | float }}
        then:
          - service: python_script.set_state
            data_template:
              entity_id: sensor.min_temp_outside
              state: "{{ states.sensor.balcone_temperatura.state }}"
          - service: python_script.set_state
            data_template:
              entity_id: sensor.min_temp_outside_time
              state: >-
                {% set t = now() %} {% set giorno =
                strptime("%0.04d-%0.02d-%0.02d %0.02d:%0.02d:%0.02d" |
                format(t.year, t.month, t.day, t.hour, t.minute, t.second),
                "%Y-%m-%d %H:%M:%S") %} {{ giorno.strftime("%H:%M:%S") }}
mode: single

I assume you know that the value set by python_script.set_state is temporary and doesn’t survive a restart (which is probably why you added the Homeassistant Start trigger).


If you are interested, it’s now possible to create Trigger-based Template Sensors to do the same thing as what you posted above but without the need for python_script.set_state, an automation, or an input_datetime.

Here’s an example that records the maximum daily temperature and the time when it occurred.

Yes the value setted by set_state in temporary.

thank you for your suggestion, im looking the script