Min Max Python Script

I wanted to have a sensor that gives me a min / max reading for the outside temperture and that resets at a given time. I looked at some of the sensors that are available in Home Assistant but at the same time I wanted to play around with python scripting. So i came up with this example.

In the configuration.yaml file add the following:

python_script:

sensor:
  - platform: rflink
    devices:
      ute_minmax:
        name: "Ute Min Max"
        sensor_type: temperature 
        unit_of_measurement: "°C"

Then I have added both a script and automation, this could all be put into an automation:
In the scripts.yaml:

update_uteminmax:
  alias: 'update ute min max'
  sequence:
    - service: python_script.temp_min_max
      data:
        entity_id: sensor.ute_temp
        trigger_id: sensor.ute_minmax
        operation: update
 clear_uteminmax:
  alias: 'clear ute min max'
  sequence:
    - service: python_script.temp_min_max
      data:
        entity_id: sensor.ute_temp
        trigger_id: sensor.ute_minmax
        operation: clear

And then in the automations.yaml:

- alias: 'Uppdatera Min Max'
  trigger:
    platform: time
    minutes: 10
  action:
    - service: script.update_uteminmax
    - service: script.update_inneminmax

- alias: 'Clear Min Max'
  trigger:
    platform: time
    at: '01:00:00'
  action:
    - service: script.clear_uteminmax
    - service: script.clear_inneminmax

And here is the temp_min_max.py that is put into the python_scripts folder:

ATTR_OPERATION = 'operation'
ATTR_OP_UPDATE = 'update'
ATTR_OP_CLEAR  = 'clear'

ATTR_ENTITY_ID = 'entity_id'
ATTR_TRIGGER_ID = 'trigger_id'

operation = data.get(ATTR_OPERATION,ATTR_OP_UPDATE)

if operation not in [ATTR_OP_UPDATE, ATTR_OP_CLEAR]:
  logger.error('Invalid operation. Expected {} or {}, got:{}'.format(ATTR_OP_UPDATE,ATTR_OP_CLEAR,operation))
else:
  entity_id = data.get(ATTR_ENTITY_ID)
  if isinstance(entity_id, str):
    entity_id = [e.strip() for e in entity_id.split(',')]
  entity_id = entity_id[0]

  trigger_id = data.get(ATTR_TRIGGER_ID)
  if isinstance(trigger_id, str):
    trigger_id = [e.strip() for e in trigger_id.split(',')]
  trigger_id = trigger_id[0]

  if operation == ATTR_OP_UPDATE:
    cur_state_minmax = hass.states.get(trigger_id)
    if cur_state_minmax is None:
      cur_state = hass.states.get(entity_id)
      if cur_state is None:
        logger.error('Could not get state of {}.'.format(entity_id))
      else:
        cur_value = float(cur_state.state)
      # First time, the minmax has no value
      # logger.error('Could not get state of {}.'.format(trigger_id))
      cur_min = cur_value
      cur_max = cur_value
    else:
      # The state is a string "min: 2.4 max: 7.9"
      cur_state_minmax_val = cur_state_minmax.state
      cur_value = cur_state_minmax_val.split()
      cur_min = float(cur_value[1])
      cur_max = float(cur_value[3])
    cur_state = hass.states.get(entity_id)
    if cur_state is None:
      logger.error('Could not get state of {}.'.format(entity_id))
    else:
      cur_value = float(cur_state.state)
      if cur_value < cur_min:
        cur_min = cur_value
      if cur_value > cur_max:
        cur_max = cur_value
      minmaxstring = "min: {} max: {}".format(cur_min,cur_max)
      hass.states.set(trigger_id,minmaxstring)
  elif operation == ATTR_OP_CLEAR:
    cur_state = hass.states.get(entity_id)
    if cur_state is None:
      logger.error('Could not get state of {}.'.format(entity_id))
    else:
      cur_value = float(cur_state.state)
      minmaxstring = "min: {} max: {}".format(cur_value,cur_value)
      hass.states.set(trigger_id,minmaxstring)

it could be helpfull to someone, but then you have to edit your text and make codeblocks from all yaml (or else it is unreadable)

and the py script is only partially as code, so you need to correct that also.