Template Help to find the last time state changed to a value

I believe the issue

Is because a float filer is used.
Make | float | int instead and it should be valid.

I’m really not having a good couple of days. Thanks Hellis.

state: >
  {% set hrs = [0.5,1,3,6][states('input_number.people_in_house')|int(1) - 1 ] %}
  {{ now() + timedelta(hours = 24 -  hrs ) }}

This will give 18 hours when 0 people are in there. I think OP should think about what needs to happen when 0 people are there because his slider can go to zero.

1 Like

Thanks all!

This is great!

And thanks @petro , I added this condition to the automation

For those following along at home, my final template sensor trigger looks like:

template:
  - trigger:
      - platform: state
        entity_id: switch.fstwifitu_switch_1
        to: "unavailable"
        for:
          minutes: 2
    sensor:
      - name: "Economy7"
        state: >
          {% set hrs = [0.5,1,3,6][states('input_number.people_in_house')|int(1) - 1 ] %}
          {{ now() + timedelta(hours = 24 -  hrs ) }}
        device_class: timestamp

This is all working fine!

However, changes to the trigger time take until the next day to take effect, because the trigger to update the template sensor.economy7 is the Wifi switch being powered off (happens once a day).

That means that if someone comes to stay on Monday, and I update the input_number.people_in_house, then the trigger updates on Monday night, and then only during the night on Tuesday will the changes to the immersion heater duration take effect. So the required amount of water will be heated and ready on Wednesday morning.

I therefore need to move the logic around start time into the automation.