Can't get conditions within a script to work - need help please

I have the following script which is working…

script:
  - id: cycle_pumps
    then:
      - logger.log: "Started First (Interval-run time) Delay"
      - delay: !lambda |-
          return (id(pump_interval) - 3 * id(temp_adj_on_time)) * 1000; 
      - switch.turn_on: pump1_relay
           
      - logger.log: "Started second (run time) Delay"
      - delay: !lambda |-
          return id(temp_adj_on_time) * 1000;
      - switch.turn_off: pump1_relay

      - switch.turn_on: pump2_relay
      - delay: !lambda |-
          return id(temp_adj_on_time) * 1000;
      - switch.turn_off: pump2_relay  

      - switch.turn_on: pump3_relay
      - delay: !lambda |-
          return id(temp_adj_on_time) * 1000;
      - switch.turn_off: pump3_relay  

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…

binary_sensor: 
  - platform: homeassistant
    name: "Pump 1 Enable"
    entity_id: input_boolean.pump1_enable
    on_state:
      then:
        - lambda: |-
            id(pump1_enabled) = x;

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

Thanks in advance

I guess this is what you’re looking for, but I have no example of my own:

This might help a bit.

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

Whoops. Edit to follow…

Did you try adding an id like this? Then you refer to id, not entity_id.

#Import HA Deep sleep control
  - platform: homeassistant
    name: "Stay Awake"
    internal: false #I want to know that everyone is talking to each other;)
    id: "stay_awake_sensor"
    entity_id: input_boolean.keep_esps_awake_switch_ha
    icon: "mdi:sleep-off"
    entity_category: diagnostic
    on_press:
      then:
        - logger.log: "STAY AWAKE requested from HA. Preventing deep sleep"
        - deep_sleep.prevent: deep_sleep_1
    on_release:
      then:
        - logger.log: "STAY AWAKE TURNED OFF. Going to sleep..."     
        - deep_sleep.enter:
            id: deep_sleep_1
            sleep_duration: ${sleep_time}

Just tried to build it with the dot in the entity ID but I get “failed config” with the same message that the dot cannot be used…

Thanks - I’ll give that a shot.

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…

script:
  - id: cycle_pumps
    then:
      - logger.log: "Started First (Interval-run time) Delay"
      - delay: !lambda |-
          return (id(pump_interval) - 3 * id(temp_adj_on_time)) * 1000; 
      - if:
          condition:
            - binary_sensor.is_on: pump1_enable 
          then:
            - switch.turn_on: pump1_relay
           
      - logger.log: "Started second (run time) Delay"
      - delay: !lambda |-
          return id(temp_adj_on_time) * 1000;
      - switch.turn_off: pump1_relay

      - if:
          condition:
            - binary_sensor.is_on: pump2_enable 
          then:
            - switch.turn_on: pump2_relay
      - delay: !lambda |-
          return id(temp_adj_on_time) * 1000;
      - switch.turn_off: pump2_relay  

      - if:
          condition:
            - binary_sensor.is_on: pump3_enable 
          then:
            - switch.turn_on: pump3_relay
      - delay: !lambda |-
          return id(temp_adj_on_time) * 1000;
      - switch.turn_off: pump3_relay  

…and the binary sensors are defined thus…

binary_sensor: 
  - platform: homeassistant
    name: "Pump 1 Enable"
    entity_id: input_boolean.pump1_enable
    id: pump1_enable
 
  - platform: homeassistant
    name: "Pump 2 Enable"
    entity_id: input_boolean.pump2_enable
    id: pump2_enable    

  - platform: homeassistant
    name: "Pump 3 Enable"
    entity_id: input_boolean.pump3_enable
    id: pump3_enable   
1 Like

It can be confusing. Generally if you follow the examples, and use a - when the item could have multiple entries at the same level , you should be ok.

Like with dot points:

  • point 1
  • point 2
  • point 3