I’m new to HA and trying to get my head around how to write a script, that is triggered by an automation, performs some calculations and decision and then steers my sprinklers. I know I could probably compose the script via the UI, but I think it would be much leaner to write it like below. This is the scenario:
I have an automation that listens to a change of a drop down and calls a script passing the selected value to the script.
in the script I want to calculate delays based on the passed value
and then switch on sprinklers
wait for the calculated delay and
switch them off
send a notification about how long the watering lasted
This is my code so far (selection Information passed by the automation to the script):
{% set durter=700 %}
{% set durs1=620 %}
{% set durs2=420 %}
{% set durbm=1240 %}
{% if selection=="Off" %}
Sprinkler are off
{% else %}
## calculate sprinkler on time for each section
{% set giesentr = ((selection | float * durter) | int | timestamp_custom('00:%M:%S')) %}
{% set giesens1 = (selection | float * durs1) | int | timestamp_custom('00:%M:%S') %}
{% set giesens2 = (selection | float * durs2) | int | timestamp_custom('00:%M:%S') %}
{% set giesenbm = (selection | float * durbm) | int | timestamp_custom('00:%M:%S') %}
{% set gesamtdur = ( giesentr + giesens1 + giesens2 + giesenbm | int timestamp_custom('00:%M:%S') %}
##steer the time on for each sprinkler based on the selection
Turn on: switch.sprinkler_terasse
delay: {{ giesentr }}
Turn of: switch.sprinkler_terasse
Turn on: switch.sprinkler_seite1
delay: {{ giesens1 }}
Turn off: switch.sprinkler_seite1
Turn on: switch.sprinkler_seite2
delay: {{ giesens2 }}
Turn off: switch.sprinkler_seite2
Turn on: switch.sprinkler_baum
delay: {{ giesenbm }}
Turn off: switch.sprinkler_baum
{% endif %}
send notification
data:
message: Everything took "{{ gesamtdur }}" minutes
title: Watering completed
So my questions are:
where withing the script yaml should I put this code?
I would strongly advise you to write a few scripts in the UI first, then you can look to see what the yaml looks like and your questions will be answered…
The dehumidifiers are not smart. They have to be switched on manually. This script turns them off by turning off the power with the smart sockets they are plugged into. It then turns the sockets on again five seconds later. Typically it would be launched from an automation, such as the one that is triggered when I leave the house.
What you have written is not a Home Assistant script. I agree with @Stiltjack, you should probably start with the UI script editor so you get the correct structure and syntax. It is very easy to set up a “skeleton” of your script in the UI editor, then switch to editing in YAML to incorporate your variable fields and templates.
selection is the parameter that is passed through to the script by triggering automation. First I check, if Off was selected and turn of the sprinkler.
If not, I start switching the sprinkler on and calculate the delay based on the selection and a variable for the specific sprinkler. And this is where I get the error, that the “Message is malformated…”
How must I write the code for the calculation of the delay?
There is no need to use | timestamp_custom('00:%M:%S') , delay accepts seconds as a integer as a valid input. How long are these delays? It usually isn’t a good idea to use delays more than a couple minutes… for prolonged waits or delays it is better to set timers or calendar events.
You are getting the error message because you are attempting to have templates inside templates and perform comparisons outside of templates… The following is a cleaned up version of what you posted, however, without addressing the long delay issue, I would not recommend using it if you need it to be reliable.