Error with history stats sensor

I get this error in the logs:

I have four history stats sensors:

  - platform: history_stats
    name: "Porttelefon count"
    entity_id: switch.porttelefon
    state: "on"
    type: count
    start: "{{ [now().replace(minute=now().minute-1), 0] | max }}"
    end: "{{ now() }}"

  - platform: history_stats
    name: gamla_plattan_tid
    entity_id: sensor.gamla_plattan_on
    state: true
    type: time
    start: '{{ now().replace(hour=0, minute=0, second=0) }}'
    end: '{{ now() }}'

  - platform: history_stats
    name: nya_plattan_tid
    entity_id: sensor.nya_plattan_on
    state: true
    type: time
    start: '{{ now().replace(hour=0, minute=0, second=0) }}'
    end: '{{ now() }}'
        
  - platform: history_stats
    name: Andreas phone on
    entity_id: binary_sensor.andreas_interactive
    state: 'on'
    type: time
    start: '{{ now().replace(hour=0, minute=0, second=0) }}'
    end: '{{ now() }}'

Three of them are only used for viewing, one is used in an automation:

- id: '1634041185587'
  alias: Porttelefon SW switch off
  description: ''
  trigger:
  - platform: numeric_state
    entity_id: sensor.porttelefon_count
    above: '12'
  condition: []
  action:
  - service: input_boolean.turn_on
    target:
      entity_id: input_boolean.porttelefon_sw_block
  - service: notify.mobile_app_andreas
    data:
      message: Porttelefonen är blockerad nu på grund av för många aktiveringar
      title: Kolla porttelefonen
      data:
        ttl: 0
        priority: high
  mode: single

What is the error about? I don’t have any int or datetime anywhere.

Every template uses a datetime accept the one that’s returning an int.

Starts an int

End is a datetime

The error comes from the zero in your max function. It needs to be a datetime.

Ahh… I see now.
So it should be

start: "{{ now().replace(minute=[now().minute-1, 0] | max ) }}"

So if now.minute is 0 then now minute -1 is less than 0 thus the template becomes 0.
That effectively means the start and end is the same but I can live with that.
It’s not that important automation, and it only happens one minute per hour.

just subtract a minute

start: "{{ now() - timedelta(minutes=1) }}"
1 Like

I have never used timedelta… But yes that would work also I guess.
Thanks!