I found out by experiment that the float
filter in jinja treats a non-existent or non-numeric input as a fatal error – i.e. it stops the automation dead. I therefore need a way of testing whether it is going to work before calling it.
The code in question is:
variables:
local_event_description: !input event_description
local_zone_calendar: !input zone_calendar
[…]
- service: input_text.set_value
data:
value: "{{ state_attr(local_zone_calendar, 'description' ) }}"
target:
entity_id: !input event_description
[…]
- service: input_number.set_value
data:
value: >-
{% set temperature_text = states(local_event_description).split('#')[1] %}
{{ '%0.1f' | format(float(temperature_text)) }}
target:
entity_id: !input event_temperature
… where
-
zone_calendar
is a local calendar with a description field that contains a temperature between hashes (and that works fine) -
event_description
is aninput_text
helper and -
event_temperature
is aninput_number
helper
The code takes the text of the description in a local calendar event and extracts a temperature specified as a floating point number to one decimal place between two hashes.
For the full code see AndySymons/heating_x.yaml
I could easily test for temperature_text being longer than 0 but how do I test whether it is a valid input to float
?