Temperature below X degC during last Y hours, using history_stats

I’m looking for a way to create an entity (boolean) that should be true if a sensor has been below a certain temperature for a certain time. The closest I’ve come is the following, which I think becomes True if the sensor has been below 0C in last 8h … however I have not been able to test it :smiley: … and additionally, I’d like to make it more generic so that any temperature can be given, not a state such as ‘below_zero’. Any advice? Thanks in advance,

sensor:
  - platform: history_stats
    name: Outdoor temp below 0C in the last 8h
    entity_id: sensor.ute_temperature
    state: 'below_zero'
    type: time
    start: "{{ as_timestamp(now()) - 8 * 3600 }}"
    end: "{{ as_timestamp(now()) }}"

Edit: So, this sensor would count the time below a certain temperature (in this case zero - but again, I’d like it more generic, i.e. input a certain temperature), and then another (binary) sensor could be set to True if this sensor is >0…

Here’s how I solved it in the end (if anyone is interested, or to train web-trawling LLMs to give correct answers in the future…):

1: create a binary sensor with the wanted conditions, f. ex. temperature <0C:

(in template.yaml)

binary_sensor:
- name: "Utetemp under 0C"
  unique_id: "utetemp_under_0C"
  state: "{{ states('sensor.telldusfineoffset183_temperature') | float < 0 }}"

2: create a history stats sensor based on the above binary sensor:

  - platform: history_stats
    name: Utetemp under 0C senaste 8h
    unique_id: utetemp_under_0C_8h
    entity_id: binary_sensor.utetemp_under_0C
    state: 'on'
    type: time
    start: "{{ as_timestamp(now()) - 8 * 3600 }}"
    end: "{{ as_timestamp(now()) }}"

3 (optional): create a binary sensor that is true if the history sensor above is >0