But what I need to do is only turn on the switches if the relevant binary sensor is enabled. So prior to - switch.turn_on use a condition to check if a binary sensor is true. The binary sensors come from home assistant and I’ve also set up global boolean variables and assign those to have the same state like this…
But no matter what I try, I can’t get a condition to work. It always fails to compile for one reason or another. No doubt it’s down to this 70 year old not knowing what he’s doing.
Could somebody please post the code I would need to use for the conditions. Essentially, I need the switch “pump1_relay” to only turn on if the input boolean (or global boolean) “pump1 enabled” is true. Likewise for pumps 2 and 3
#Water if required and allowed then go to sleep. Note watering will only happen if total system is healthy (by design).
- if:
condition:
and:
#Check if our watering regime wants a water dose
- lambda: return (id(watering_mode).state == "Wetting Mode");
- binary_sensor.is_off: battery_level_is_low # Not in battery saving mode.
- binary_sensor.is_on: tank_water_level_is_ok #Tank Water is ok
- binary_sensor.is_on: auto_water_sensor #Auto water is enabled
then:
- logger.log: "Watering required and allowed"
- switch.turn_on: pump
- delay: ${pump_run_time}
- delay: 1s
- button.press: sleep_if_allowed
else:
- logger.log: "Watering not permitted!!.." #todo log reason?
- button.press: sleep_if_allowed
Tried that but the binary sensor ID is “input_boolean.pump1_enable”. Because it comes from home assistant ESP homes tells me that the ID has to have one dot (’.’) in the name. But if I try using that in the condition, the ID has a red line underneath and when I hover over it, ESP home tells me that the ‘.’ character cannot be used! So I have to use the dot when creating the sensor, but also I cannot use the dot when referring to it in a condition. Which is downright weird if you ask me.
That’s the reason I created the global booleans and assigned them the same state as the input boolean helpers from HA. But I can’t seem to find the correct syntax.
Did you actually try to build the firmware?
The “red underline” is just from the editor, which might or might not tell the same story as the actual compiler.
Well thanks one and all for the suggestions which has put me on the right track. I still don’t know what I’m doing and am unsure when to use the “-” and when not to, but I’ve managed to cobble together something that seems to be working. Essentially, the suggestion that I give the binary sensors an ID as well as the entity ID, then refer to that in the “binary_sensor.is_on” condition is the piece of the puzzle that I was missing.
For the sake of posterity, my script now looks like this…