Template evaluates correctly in developer, but fails in conditional code

I have a template to select a sensor to test, among several choices, based on an input number sensor selector. It evaluates correctly in developer, showing the correct sensor based on input and its state. however when I use it in a conditional, either as an if, then else, or choose condition, it fails to function.

alias: 13A_EV_as_set
description: conditional turn on ev set on input time
trigger:
  - platform: time_pattern
    minutes: "00"
    seconds: "30"
  - platform: time_pattern
    minutes: "30"
    seconds: "30"
condition: []
action:
  - choose:
      - conditions:
          - condition: template
            value_template: >-
              {{ states('binary_sensor.octopus_energy_target_agile_' ~
              '{:02d}'.format(states('input_number.sensor_selector') | int)) }}
        sequence:
          - type: turn_on
            device_id: aafd41*
            entity_id: 09592b*
            domain: switch
    default:
      - type: turn_off
        device_id: aafd41ecf*
        entity_id: 09592bc1b*
        domain: switch
mode: single

the developer template shows

Blockquote

Result type: string

on

This template listens for the following state changed events:

  • Entity: binary_sensor.octopus_energy_target_agile_24
  • Entity: input_number.sensor_selector"

Blockquote

Ive tried HA documentation and the community, but failed to find an explanation.

help and advice would be appreciated

Thanks

S

It’s confusing to me what you are trying to get out of that template.

the state of a binary sensor is either ‘on’ or ‘off’ and it’s also a string.

then you are concatenating that string to the the state of an input number which you turned into an integer.

I’m not sure that can work.

TBH, I’m surprised that it doesn’t throw an error. Unless the template editor is doing some behind the scenes magic to make it work.

I have a number of sensors to pick up variable tariff electricity rates, eg binary_sensor.octopus_energy_target_agile_01, binary_sensor.octopus_energy_target_agile_02, etc

the input sensor allows me to select the desired time, say 2 hrs and the template puts that number into the sensor name as 02.

when I test the template in developer tools it works as expected

when I incorporate it into a simple automation with a condition, it works correctly

alias: test 13a
description: conditional turn on ev set on input time
trigger:
  - platform: time_pattern
    minutes: "00"
    seconds: "30"
  - platform: time_pattern
    minutes: "30"
    seconds: "30"
condition:
  - condition: template
    value_template: >
      {{ states('binary_sensor.octopus_energy_target_agile_' ~
      '{:02d}'.format(states('input_number.sensor_selector') | int)) }}
action:
  - type: turn_on
    device_id: aafd41ecf098e1bbd20e112c7db123d4
    entity_id: 09592bc1be0838f01d03f2ed63b0741c
    domain: switch
mode: single

but if I add a default, or an if or a choice command, it fails to evaluate to true when it should and the other action runs, which turns off the switch. either using visual editor, or trying to write the code.

your comment that the template should not really work, makes me wonder if it isn’t correctly written and fails in more complex settings. I’m new to this & have struggled to translate the code documentation into practical applications.

Thanks
S

choose:
  - conditions:
      - condition: template
        value_template: >-
          {{ states('binary_sensor.octopus_energy_target_agile_' ~
          '{:02d}'.format(states('input_number.sensor_selector') | int)) }}
    sequence:
      - type: turn_on
        device_id: aafd41ecf098e1bbd20e112c7db123d4
        entity_id: 09592bc1be0838f01d03f2ed63b0741c
        domain: switch
default:
  - type: turn_off
    device_id: aafd41ecf098e1bbd20e112c7db123d4
    entity_id: 09592bc1be0838f01d03f2ed63b0741c
    domain: switch

hrs set to 24 and sensor is ON

If I just use the entity id for the sensor, rather than a template, the conditional action works fine

alias: 12_ev
description: 12 hrs intermittent EV
trigger:
  - platform: time_pattern
    minutes: "2"
  - platform: time_pattern
    minutes: "32"
condition: []
action:
  - if:
      - condition: state
        state: "on"
        entity_id: binary_sensor.octopus_energy_target_agile_12
    then:
      - type: turn_on
        device_id: a421532e599abdc896ed811a32adaa48
        entity_id: c1ecd9068316fe97f7128ed1a8a09e56
        domain: switch
    else:
      - type: turn_off
        device_id: a421532e599abdc896ed811a32adaa48
        entity_id: c1ecd9068316fe97f7128ed1a8a09e56
        domain: switch
mode: single

Have you tried filtering it into a boolean?

{{ states('binary_sensor.octopus_energy_target_agile_' ~
'{:02d}'.format(states('input_number.sensor_selector') | int)) | bool }}

not tried Boolean; what would that do?

S

Without bool: Condition fails with just the string “on”.

With bool: Condition passes.

1 Like

Thank you. That looks really helpful.

Am I understanding correctly from the template docs

  • bool(value, default) function converts the value to either true or false. The following values are considered to be true: boolean true, non-zero ints and floats, and the strings "true", "yes", "on", "enable", and "1" (case-insensitive). false is returned for the opposite values: boolean false, integer or floating-point 0, and the strings "false", "no", "off", "disable", and "0" (also case-insensitive). If the value is not listed here, the function returns the default value, or if omitted raises an error. This function is intended to be used on states of binary sensors, switches, or similar entities, so its behavior is different from Python’s built-in bool conversion, which would consider e.g. "on", "off", and "unknown" all to be true, but "" to be false; if that is desired, use not not value or a similar construct instead. Like float and int, bool has a filter form. Using none as the default value is particularly useful in combination with the immediate if filter: it can handle all three possible cases in a single line.

that bool is forcing the evaluation of the template string to a true/false that triggers the condition correctly, whereas ive been generating an On state that is not parsed correctly by the conditional syntax?

S

PS. Brilliant - works as it should. Thank you!