Blueprint and duration

I’m struggling with making a modification to an existing blueprint by adding a duration input.

I have a duration input that later on I want to use on a calculation (comparing duration to duration) but I think variables are string.

This works:
remaining > timedelta(minutes=10)

But the approach I’m trying on the snippet below doesn’t work:
remaining > button1_duration

A snippet of the blueprint, containing only the parts I’m having issues with:

blueprint:
[...]
  input:
    button1_duration:
      name: Button 1 Duration (Top Left)
      default: 0:10:00
      selector:
        duration:
[...]
variables:
[...]
  button1_duration: !input button1_duration
[...]
trigger:
[...]
action:
- choose:
  - conditions:
    - condition: trigger
      id: state_UNKNOWN
    sequence:
    - choose:
      - conditions:
        - condition: template
          value_template: '{% set remaining = timedelta(seconds=(as_timestamp(state_attr(fan_timer,
            ''finishes_at'')) - as_timestamp(now())) | int) %}{{ remaining > button1_duration
            }}'
[...]

I hope this makes sense to the most experienced guys here and it’s a simple thing I just can’t figure out.

Thanks!

apparently the duration is a dictionary.

variables:
[...]
  bds: !input button1_duration
  button1_duration:  >-
    {{ timedelta(hours=bds.hours, minutes=bds.minutes, seconds=bds.seconds) }}
[...]
trigger:
[...]
action:
- choose:
  - conditions:
    - condition: trigger
      id: state_UNKNOWN
    sequence:
    - choose:
      - conditions:
        - condition: template
          value_template: '{% set remaining = state_attr(fan_timer, ''finishes_at'') | as_datetime | as_local - now() %}{{ remaining > button1_duration }}'

Also, I fixed your remaining syntax. You were comparing an int to a timedelta. When you subtract two datetimes, they make a timedelta.

Also, you can optimize what I wrote, but it might not make sense to you.

variables:
[...]
  bds: !input button1_duration
  button1_duration:  >-
    {{ timedelta(**bds) }}
1 Like

Thanks, @petro!

We’re getting closer, but I’m still getting errors in the action:
unsupported type for timedelta minutes component: dict

"Unable to compute proper state for state_UNKNOWN for switch.mbath_fan with scene controller 41fae806b8d154209b024b9314573178."
9:49:47 AM – (ERROR) components/system_log/__init__.py - message first occurred at September 14, 2022 at 3:45:11 PM and shows up 14 times

Error in 'choose[0]' evaluation: In 'template' condition: TypeError: unsupported type for timedelta minutes component: dict
9:49:47 AM – (WARNING) helpers/script.py - message first occurred at September 14, 2022 at 3:45:11 PM and shows up 25 times

Do you see anything wrong in this code?
I’ve adjusted the value_template code to what you sent as it’s cleaner, but it looks like it’s the same error as before:

- choose:
  - conditions:
    - condition: trigger
      id: state_UNKNOWN
    sequence:
    - choose:
      - conditions:
        - condition: template
          value_template: '{% set remaining = state_attr(fan_timer, ''finishes_at'') | as_datetime | as_local - now() %}{{ remaining > timedelta(minutes=30) }}'
        sequence:
        - event: ZEN32_EXHAUST_FAN_BLUEPRINT_STATE_CHANGE
          event_data:
            scene_controller: !input scene_controller
            fan_switch: !input fan_switch
            fan_timer: !input fan_timer
            new_state: ON_GT_45_MIN
      - conditions:
        - condition: template
          value_template: '{% set remaining = state_attr(fan_timer, ''finishes_at'') | as_datetime | as_local - now() %}{{ remaining > timedelta(minutes=15) and remaining <= timedelta(minutes=30) }}'
        sequence:
        - event: ZEN32_EXHAUST_FAN_BLUEPRINT_STATE_CHANGE
          event_data:
            scene_controller: !input scene_controller
            fan_switch: !input fan_switch
            fan_timer: !input fan_timer
            new_state: ON_GT_30_LTE_45_MIN
      - conditions:
        - condition: template
          value_template: '{% set remaining = state_attr(fan_timer, ''finishes_at'') | as_datetime | as_local - now() %}{{ remaining > timedelta(minutes=10) and remaining <= timedelta(minutes=20) }}'
        sequence:
        - event: ZEN32_EXHAUST_FAN_BLUEPRINT_STATE_CHANGE
          event_data:
            scene_controller: !input scene_controller
            fan_switch: !input fan_switch
            fan_timer: !input fan_timer
            new_state: ON_GT_20_LTE_30_MIN
      - conditions:
        - condition: template
          value_template: '{% set remaining = state_attr(fan_timer, ''finishes_at'') | as_datetime | as_local - now() %}{{ remaining > button1_duration and remaining <= timedelta(minutes=10) }}'
        sequence:
        - event: ZEN32_EXHAUST_FAN_BLUEPRINT_STATE_CHANGE
          event_data:
            scene_controller: !input scene_controller
            fan_switch: !input fan_switch
            fan_timer: !input fan_timer
            new_state: ON_GT_10_LTE_20_MIN
      - conditions:
        - condition: template
          value_template: '{% set remaining = state_attr(fan_timer, ''finishes_at'') | as_datetime | as_local - now() %}{{ remaining <= button1_duration }}'
        sequence:
        - event: ZEN32_EXHAUST_FAN_BLUEPRINT_STATE_CHANGE
          event_data:
            scene_controller: !input scene_controller
            fan_switch: !input fan_switch
            fan_timer: !input fan_timer
            new_state: ON_LTE_10_MIN
      default:
      - service: system_log.write
        data_template:
          level: error
          logger: zen32_exhaust_fan_blueprint
          message: '"Unable to compute proper state for {{ trigger.id }} for {{ fan_switch }} with scene controller {{ scene_controller }}."'

Could it be possible that the fan_timer may not be initialized and doesn’t have a finishes_at value?

Because, otherwise, the code seems to work:

I was able to make it work!
Sharing in case somebody else runs into a similar issue:

This won’t work:

variables:
[...]
  bds: !input button1_duration
  button1_duration:  >-
    {{ timedelta(**bds) }}
[...]
trigger:
[...]
action:
- choose:
  - conditions:
    - condition: trigger
      id: state_UNKNOWN
    sequence:
    - choose:
      - conditions:
        - condition: template
          value_template: '{% set remaining = state_attr(fan_timer, ''finishes_at'') | as_datetime | as_local - now() %}{{ remaining > button1_duration }}'

But this works:

variables:
[...]
  button1_duration: !input button1_duration
[...]
trigger:
[...]
action:
- choose:
  - conditions:
    - condition: trigger
      id: state_UNKNOWN
    sequence:
    - choose:
      - conditions:
        - condition: template
          value_template: '{% set remaining = state_attr(fan_timer, ''finishes_at'') | as_datetime | as_local - now() %}{{ remaining > timedelta(button1_duration) }}'