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.
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.
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?