Input_number Template help

I am trying to convert my hard coded irrigation automation to use and input_number helper.
I do not understand the templating syntax for input_numbers - what is the correct syntax?

alias: Sprinkler Water  Cactus hard coded
description: ''
trigger:
  - at: '10:00:00'
    platform: time
condition:
  - condition: template
    value_template: '{{ now().strftime(''%j'') | int % 14 == 0}}'
action:
  - data: {}
    service: script.turn_on
    target:
      entity_id: script.sprinkler_cactus
mode: single

I want to convert the condition to use and input_number instead of the hard coded 14

condition:
  - condition: template
    value_template: '{{ now().strftime(''%j'') | int % states(''input_number.cactus_days'') == 0}}'

that structure is not passing the test button. → template value should be a string for dictionary value @ data[‘value_template’]. Got None

condition:
  - condition: template
    value_template: '{{ now().timetuple().tm_yday % states(''input_number.cactus_days'') | int(0) == 0 }}'

thank you but, sadly, that gives me the same error:


template value should be a string for dictionary value @ data['value_template']. Got None
  condition:
  - condition: template
    value_template: '{{ now().timetuple().tm_yday % states(''input_number.cactus_days'') | int(0) == 0 }}'

If you’re testing the template using the Automation Editor’s Test button, I have bad news. It has a bug and is liable to report errors where none exist.

Copy-paste the template into the Template Editor and test it.

it doesn’t work in the template editor either. quite probably because i don’t understand enough to convert the syntax between the two.

{{ now().timetuple().tm_yday % states(''input_number.cactus_days'') | int(0) == 0 }}

that fails with or without quotes around it.

'{{ now().timetuple().tm_yday % states(''input_number.cactus_days'') | int(0) == 0 }}'

or with":

value_template: '{{ now().timetuple().tm_yday % states(''input_number.cactus_days'') | int(0) == 0 }}'

in the mean time I have enabled the automation and will see if it executes at 10am (22 minutes from now)

The Template Editor doesn’t like the doubled single-quotes ('') preferred by the Automation Editor. It accepts single-quotes ('), double-quotes ("), but not doubled single-quotes.

Try this in the Template Editor. It uses double-quotes.

{{ now().timetuple().tm_yday % states("input_number.cactus_days") | int(0) == 0 }}

If it still fails, post the reported error message.

It works! just ran it. Thanks all !

- id: '1654731004178'
  alias: Sprinkler Water  Cactus input number
  description: ''
  trigger:
  - at: '10:00:00'
    platform: time
  condition:
  - condition: template
    value_template: '{{ now().timetuple().tm_yday % states(''input_number.cactus_days'')
      | int(0) == 0 }}'
  action:
  - data: {}
    service: script.turn_on
    target:
      entity_id: script.sprinkler_cactus

that works in the template editor - thank you

{{ now().timetuple().tm_yday % states("input_number.cactus_days") | int(0) == 0 }}

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information refer to guideline 21 in the FAQ.

the final code looks list this:

alias: Sprinkler Water  Cactus
description: ''
trigger:
  - at: input_datetime.cactus_water_stsart
    platform: time
condition:
  - condition: template
    value_template: >-
      {{ now().timetuple().tm_yday % states('input_number.cactus_days') | int(0)
      == 0 }}
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id:
        - switch.back_cactus
        - switch.front_cactus
  - delay: '{{ states(''input_number.cactus_time'') | multiply(60) | int }}'
  - service: switch.turn_off
    data: {}
    target:
      entity_id:
        - switch.back_cactus
        - switch.front_cactus

You marked your own post as the Solution yet it contains what I had suggested you should use. That’s not how the community uses the Solution tag. It’s assigned to the first, or best, post that identifies the cause of the problem and offers a corrective procedure. Please read the linked FAQ.

oops, sorry about that. I hope I clicked the correct one this time

1 Like

What’s the typical duration of the delay? Just a few minutes or much longer?

The reason why I ask is because if Home Assistant is restarted while the delay is counting down, it will be cancelled. It will also be cancelled whenever you execute Reload Automations and that happens in the background every time you click the Save button in the Automation Editor.

If it’s a short duration then the window of opportunity to be accidentally cancelled is narrow. But if it’s a long duration then its more vulnerable to a restart or reload. There are ways to redesign the automation without using a delay that make the automation more robust (unaffected by restart/reload).

up to about 20 minutes, so yes there can be race conditions like that. I have put in watch dog timers in all of the irrigation switches via esp_home’s feature

    # Set automatic timeout 20 minutes
    on_turn_on:
      then:
        - delay: 20min
        - switch.turn_off: UpperGarden

I would love to see your ideas for working around this issue.
( I did have a drunk test this last year when they hit a power pole and put the server in an unrunable condition. Since then I’ve also added UPSs to all my systems. )

OK, then there’s no danger of them ever being left on for hours.

Basically you would use a timer entity. When you create the timer, ensure you enable its restore option.

Start the timer (using the timer.start service call) either just before or after you execute switch.turn_on. Set the timer’s duration using the value of input_number.cactus_time.

Add an Event Trigger to detect the timer’s timer.finished event then execute switch.turn_off.