Create automation to run switch for a given number of minutes based on a helper number input with derived time

Looking to set an automation that will run my pump for x number of minutes (decimal mins) based on the relationship between gallons entered into a helper input_number and the following equation:

decimal run time in minutes = gallons / 1.66

Any ideas?

What is the scale of this?
Is the pump running for minutes or hours?
If it’s just a short run then you could delay between on and off, otherwise you would need a different trigger.

I have an automation that will run currently with a delay in the code… I am wanting that delay to be driven by the input into an input_number, and then logic to convert the gal to either decimal mins through the div by 1.66 or a way of converting the gal input to hh:mm:ss.

The delay ranges currently from 00:02:15 through 02:24:00

alias: Add Chlorine (gal input)
description: ''
trigger:
  - platform: state
    entity_id:
      - input_button.add_per_input_box_gal
condition: []
action:
  - if:
      - condition: state
        entity_id: switch.pentair_18_41_ca_high_speed
        state: 'on'
    then:
      - service: switch.turn_on
        data: {}
        target:
          entity_id: switch.pentair_chlor
      - delay:
          hours: 0
          minutes: 2
          seconds: 15
          milliseconds: 0
      - service: switch.turn_off
        data: {}
        target:
          entity_id: switch.pentair_chlor
    else:
      - service: switch.turn_on
        data: {}
        target:
          entity_id: switch.pentair_18_41_ca_high_speed
      - delay:
          hours: 0
          minutes: 1
          seconds: 0
          milliseconds: 0
      - service: switch.turn_on
        data: {}
        target:
          entity_id: switch.pentair_chlor
      - delay:
          hours: 0
          minutes: 2
          seconds: 15
          milliseconds: 0
      - service: switch.turn_off
        data: {}
        target:
          entity_id: switch.pentair_chlor
mode: single

The - delay: is where i want to add script to do the conversion from an input_number to hh:mm:ss

You should be able to use a delay like:

      - delay:
          hours: 0
          minutes: "{{ (( states('input_number.number') | float ) / 1.66 ) | int }}"
          seconds: 0
          milliseconds: 0

But I don’t recommend this method since a reload or restart means the automation breaks.

Instead you could set an input_datetime of when to switch off and trigger on this datetime.
You set a datetime like this for example:

service: input_datetime.set_datetime
data:
  timestamp: "{{ ((( states('input_number.number') | float ) / 1.66 ) + now().timestamp() ) | int }}"
target:
  entity_id: input_datetime.switch_off_time

I think what you need is something like this:

alias: Add Chlorine (gal input)
description: ''
trigger:
  - platform: state
    entity_id:
      - input_button.add_per_input_box_gal
    id: button
  - platform: time
    at: input_datetime.switch_off_time
    id: time
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: button
        sequence:
          - service: switch.turn_on
            data: {}
            target:
              entity_id: switch.pentair_18_41_ca_high_speed
          - service: input_datetime.set_datetime
            data:
              timestamp: "{{ ((( states('input_number.number') | float ) / 1.66 ) + now().timestamp() ) | int }}"
            target:
              entity_id: input_datetime.switch_off_time
    default: []
  - choose:
      - conditions:
          - condition: trigger
            id: time
        sequence:
          - service: switch.turn_off
            data: {}
            target:
              entity_id: switch.pentair_chlor
    default: []

Thanks, this is awesome. Nice to have someone help, even though I ‘don’t know code’

So I have got this into my config, and have created buttons and input boxes, but I am having issues running the automation.

Here is the trace:

{
  "trace": {
    "last_step": "action/0/choose/0/sequence/1",
    "run_id": "4fe5629b8bfc5915cd1900818b327e88",
    "state": "stopped",
    "script_execution": "error",
    "timestamp": {
      "start": "2022-07-21T16:25:33.673478+00:00",
      "finish": "2022-07-21T16:25:33.759021+00:00"
    },
    "domain": "automation",
    "item_id": "1658420676496",
    "error": "Error rendering data template: ValueError: Template error: float got invalid input 'unknown' when rendering template '{{ ((( states('input_number.chlorine_to_add') | float ) / 1.66666666666667 * 16 ) + now().timestamp() ) | int }}' but no default was specified",
    "trigger": "state of input_button.add_per_input_box_cup",
    "trace": {
      "trigger/0": [
        {
          "path": "trigger/0",
          "timestamp": "2022-07-21T16:25:33.673607+00:00",
          "changed_variables": {
            "this": {
              "entity_id": "automation.add_chlorine_cup_input",
              "state": "on",
              "attributes": {
                "last_triggered": null,
                "mode": "single",
                "current": 0,
                "id": "1658420676496",
                "friendly_name": "Add Chlorine (cup input)"
              },
              "last_changed": "2022-07-21T16:24:36.905471+00:00",
              "last_updated": "2022-07-21T16:24:36.905471+00:00",
              "context": {
                "id": "01G8GS6J99ZGEZ701SEWC5SPJT",
                "parent_id": null,
                "user_id": null
              }
            },
            "trigger": {
              "id": "button",
              "idx": "0",
              "platform": "state",
              "entity_id": "input_button.add_per_input_box_cup",
              "from_state": null,
              "to_state": {
                "entity_id": "input_button.add_per_input_box_cup",
                "state": "unknown",
                "attributes": {
                  "editable": true,
                  "icon": "mdi:water-opacity",
                  "friendly_name": "Add Per Input Box (cup)"
                },
                "last_changed": "2022-07-21T16:25:33.670967+00:00",
                "last_updated": "2022-07-21T16:25:33.670967+00:00",
                "context": {
                  "id": "01G8GS89Q69Z2PJPVVK32MEDN7",
                  "parent_id": null,
                  "user_id": null
                }
              },
              "for": null,
              "attribute": null,
              "description": "state of input_button.add_per_input_box_cup"
            }
          }
        }
      ],
      "action/0": [
        {
          "path": "action/0",
          "timestamp": "2022-07-21T16:25:33.675951+00:00",
          "changed_variables": {
            "context": {
              "id": "01G8GS89Q9A2SW3EV7YFT992F7",
              "parent_id": "01G8GS89Q69Z2PJPVVK32MEDN7",
              "user_id": null
            }
          },
          "error": "Error rendering data template: ValueError: Template error: float got invalid input 'unknown' when rendering template '{{ ((( states('input_number.chlorine_to_add') | float ) / 1.66666666666667 * 16 ) + now().timestamp() ) | int }}' but no default was specified",
          "result": {
            "choice": 0
          }
        }
      ],
      "action/0/choose/0": [
        {
          "path": "action/0/choose/0",
          "timestamp": "2022-07-21T16:25:33.676377+00:00",
          "result": {
            "result": true
          }
        }
      ],
      "action/0/choose/0/conditions/0": [
        {
          "path": "action/0/choose/0/conditions/0",
          "timestamp": "2022-07-21T16:25:33.677781+00:00",
          "result": {
            "result": true
          }
        }
      ],
      "action/0/choose/0/sequence/0": [
        {
          "path": "action/0/choose/0/sequence/0",
          "timestamp": "2022-07-21T16:25:33.678481+00:00",
          "result": {
            "params": {
              "domain": "switch",
              "service": "turn_on",
              "service_data": {},
              "target": {
                "entity_id": [
                  "switch.pentair_18_41_ca_high_speed"
                ]
              }
            },
            "running_script": false,
            "limit": 10
          }
        }
      ],
      "action/0/choose/0/sequence/1": [
        {
          "path": "action/0/choose/0/sequence/1",
          "timestamp": "2022-07-21T16:25:33.743343+00:00",
          "error": "Error rendering data template: ValueError: Template error: float got invalid input 'unknown' when rendering template '{{ ((( states('input_number.chlorine_to_add') | float ) / 1.66666666666667 * 16 ) + now().timestamp() ) | int }}' but no default was specified"
        }
      ]
    },
    "config": {
      "id": "1658420676496",
      "alias": "Add Chlorine (cup input)",
      "description": "",
      "trigger": [
        {
          "platform": "state",
          "entity_id": [
            "input_button.add_per_input_box_cup"
          ],
          "id": "button"
        },
        {
          "platform": "time",
          "at": "input_datetime.switch_off_time",
          "id": "time"
        }
      ],
      "condition": [],
      "action": [
        {
          "choose": [
            {
              "conditions": [
                {
                  "condition": "trigger",
                  "id": "button"
                }
              ],
              "sequence": [
                {
                  "service": "switch.turn_on",
                  "data": {},
                  "target": {
                    "entity_id": "switch.pentair_18_41_ca_high_speed"
                  }
                },
                {
                  "service": "input_datetime.set_datetime",
                  "data": {
                    "timestamp": "{{ ((( states('input_number.chlorine_to_add') | float ) / 1.66666666666667 * 16 ) + now().timestamp() ) | int }}"
                  },
                  "target": {
                    "entity_id": "input_datetime.switch_off_time"
                  }
                }
              ]
            }
          ],
          "default": []
        },
        {
          "choose": [
            {
              "conditions": [
                {
                  "condition": "trigger",
                  "id": "time"
                }
              ],
              "sequence": [
                {
                  "service": "switch.turn_off",
                  "data": {},
                  "target": {
                    "entity_id": "switch.pentair_chlor"
                  }
                }
              ]
            }
          ],
          "default": []
        }
      ],
      "mode": "single"
    },
    "blueprint_inputs": null,
    "context": {
      "id": "01G8GS89Q9A2SW3EV7YFT992F7",
      "parent_id": "01G8GS89Q69Z2PJPVVK32MEDN7",
      "user_id": null
    }
  },
  "logbookEntries": [
    {
      "name": "Add Chlorine (cup input)",
      "message": "triggered by state of input_button.add_per_input_box_cup",
      "source": "state of input_button.add_per_input_box_cup",
      "entity_id": "automation.add_chlorine_cup_input",
      "context_id": "01G8GS89Q9A2SW3EV7YFT992F7",
      "when": 1658420733.673788,
      "domain": "automation"
    }
  ]
}

Any ideas?

Is this entity correct?
input_number.chlorine_to_add

I made some changes… here is the automation:

alias: Add Chlorine (cup input)
description: ''
trigger:
  - platform: state
    entity_id:
      - input_button.add_per_input_box_cup
    id: button
  - platform: time
    at: input_datetime.switch_off_time
    id: time
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: button
        sequence:
          - service: switch.turn_on
            data: {}
            target:
              entity_id: switch.pentair_18_41_ca_high_speed
          - service: input_datetime.set_datetime
            data:
              timestamp: >-
                {{ ((( states('input_number.cup_s') | float ) / 1.66666666666667
                / 16 ) + now().timestamp() ) | int }}
            target:
              entity_id: input_datetime.switch_off_time
    default: []
  - choose:
      - conditions:
          - condition: trigger
            id: time
        sequence:
          - service: switch.turn_off
            data: {}
            target:
              entity_id: switch.pentair_chlor
    default: []
mode: single

All entities are correct in this code.

Here is the trace, no error now but the switch is not triggering.

{
  "trace": {
    "last_step": "action/1/choose/0/conditions/0",
    "run_id": "4b4ec0084e13956be55b375e7a79a652",
    "state": "stopped",
    "script_execution": "finished",
    "timestamp": {
      "start": "2022-07-21T19:25:08.014879+00:00",
      "finish": "2022-07-21T19:25:08.115682+00:00"
    },
    "domain": "automation",
    "item_id": "1658420676496",
    "trigger": "state of input_button.add_per_input_box_cup",
    "trace": {
      "trigger/0": [
        {
          "path": "trigger/0",
          "timestamp": "2022-07-21T19:25:08.014957+00:00",
          "changed_variables": {
            "this": {
              "entity_id": "automation.add_chlorine_cup_input",
              "state": "on",
              "attributes": {
                "last_triggered": "2022-07-21T19:22:10.527773+00:00",
                "mode": "single",
                "current": 0,
                "id": "1658420676496",
                "friendly_name": "Add Chlorine (cup input)"
              },
              "last_changed": "2022-07-21T19:23:35.473746+00:00",
              "last_updated": "2022-07-21T19:23:35.473746+00:00",
              "context": {
                "id": "01G8H3E95HYKC54XG0FQHB0X9M",
                "parent_id": null,
                "user_id": null
              }
            },
            "trigger": {
              "id": "button",
              "idx": "0",
              "platform": "state",
              "entity_id": "input_button.add_per_input_box_cup",
              "from_state": {
                "entity_id": "input_button.add_per_input_box_cup",
                "state": "2022-07-21T19:22:10.523716+00:00",
                "attributes": {
                  "editable": true,
                  "icon": "mdi:water-opacity",
                  "friendly_name": "Add Per Input Box (cup)"
                },
                "last_changed": "2022-07-21T19:23:26.597704+00:00",
                "last_updated": "2022-07-21T19:23:26.597704+00:00",
                "context": {
                  "id": "01G8H3E0G50XDTVW1CQ2E4HK8M",
                  "parent_id": null,
                  "user_id": null
                }
              },
              "to_state": {
                "entity_id": "input_button.add_per_input_box_cup",
                "state": "2022-07-21T19:25:08.013410+00:00",
                "attributes": {
                  "editable": true,
                  "icon": "mdi:water-opacity",
                  "friendly_name": "Add Per Input Box (cup)"
                },
                "last_changed": "2022-07-21T19:25:08.013558+00:00",
                "last_updated": "2022-07-21T19:25:08.013558+00:00",
                "context": {
                  "id": "01G8H3H3H9ND3BFZXN08SGK7J4",
                  "parent_id": null,
                  "user_id": "4bd34cc657944781af68fc1844948a77"
                }
              },
              "for": null,
              "attribute": null,
              "description": "state of input_button.add_per_input_box_cup"
            }
          }
        }
      ],
      "action/0": [
        {
          "path": "action/0",
          "timestamp": "2022-07-21T19:25:08.016957+00:00",
          "changed_variables": {
            "context": {
              "id": "01G8H3H3HEKJ3VCP6PASEHK9Z3",
              "parent_id": "01G8H3H3H9ND3BFZXN08SGK7J4",
              "user_id": null
            }
          },
          "result": {
            "choice": 0
          }
        }
      ],
      "action/0/choose/0": [
        {
          "path": "action/0/choose/0",
          "timestamp": "2022-07-21T19:25:08.018501+00:00",
          "result": {
            "result": true
          }
        }
      ],
      "action/0/choose/0/conditions/0": [
        {
          "path": "action/0/choose/0/conditions/0",
          "timestamp": "2022-07-21T19:25:08.018562+00:00",
          "result": {
            "result": true
          }
        }
      ],
      "action/0/choose/0/sequence/0": [
        {
          "path": "action/0/choose/0/sequence/0",
          "timestamp": "2022-07-21T19:25:08.019251+00:00",
          "result": {
            "params": {
              "domain": "switch",
              "service": "turn_on",
              "service_data": {},
              "target": {
                "entity_id": [
                  "switch.pentair_18_41_ca_high_speed"
                ]
              }
            },
            "running_script": false,
            "limit": 10
          }
        }
      ],
      "action/0/choose/0/sequence/1": [
        {
          "path": "action/0/choose/0/sequence/1",
          "timestamp": "2022-07-21T19:25:08.089495+00:00",
          "result": {
            "params": {
              "domain": "input_datetime",
              "service": "set_datetime",
              "service_data": {
                "timestamp": 1658431508,
                "entity_id": [
                  "input_datetime.switch_off_time"
                ]
              },
              "target": {
                "entity_id": [
                  "input_datetime.switch_off_time"
                ]
              }
            },
            "running_script": false,
            "limit": 10
          }
        }
      ],
      "action/1": [
        {
          "path": "action/1",
          "timestamp": "2022-07-21T19:25:08.112821+00:00",
          "result": {
            "choice": "default"
          }
        }
      ],
      "action/1/choose/0": [
        {
          "path": "action/1/choose/0",
          "timestamp": "2022-07-21T19:25:08.112953+00:00",
          "result": {
            "result": false
          }
        }
      ],
      "action/1/choose/0/conditions/0": [
        {
          "path": "action/1/choose/0/conditions/0",
          "timestamp": "2022-07-21T19:25:08.113008+00:00",
          "result": {
            "result": false
          }
        }
      ]
    },
    "config": {
      "id": "1658420676496",
      "alias": "Add Chlorine (cup input)",
      "description": "",
      "trigger": [
        {
          "platform": "state",
          "entity_id": [
            "input_button.add_per_input_box_cup"
          ],
          "id": "button"
        },
        {
          "platform": "time",
          "at": "input_datetime.switch_off_time",
          "id": "time"
        }
      ],
      "condition": [],
      "action": [
        {
          "choose": [
            {
              "conditions": [
                {
                  "condition": "trigger",
                  "id": "button"
                }
              ],
              "sequence": [
                {
                  "service": "switch.turn_on",
                  "data": {},
                  "target": {
                    "entity_id": "switch.pentair_18_41_ca_high_speed"
                  }
                },
                {
                  "service": "input_datetime.set_datetime",
                  "data": {
                    "timestamp": "{{ ((( states('input_number.cup_s') | float ) / 1.66666666666667 / 16 ) + now().timestamp() ) | int }}"
                  },
                  "target": {
                    "entity_id": "input_datetime.switch_off_time"
                  }
                }
              ]
            }
          ],
          "default": []
        },
        {
          "choose": [
            {
              "conditions": [
                {
                  "condition": "trigger",
                  "id": "time"
                }
              ],
              "sequence": [
                {
                  "service": "switch.turn_off",
                  "data": {},
                  "target": {
                    "entity_id": "switch.pentair_chlor"
                  }
                }
              ]
            }
          ],
          "default": []
        }
      ],
      "mode": "single"
    },
    "blueprint_inputs": null,
    "context": {
      "id": "01G8H3H3HEKJ3VCP6PASEHK9Z3",
      "parent_id": "01G8H3H3H9ND3BFZXN08SGK7J4",
      "user_id": null
    }
  },
  "logbookEntries": [
    {
      "name": "Add Chlorine (cup input)",
      "message": "triggered by state of input_button.add_per_input_box_cup",
      "source": "state of input_button.add_per_input_box_cup",
      "entity_id": "automation.add_chlorine_cup_input",
      "context_id": "01G8H3H3HEKJ3VCP6PASEHK9Z3",
      "when": 1658431508.015135,
      "domain": "automation"
    }
  ]
}

I’m really bad at reading traces.
Did the datetime got set?

Is it the correct switch?
I see you have switch.pentair_chlor in your automation also.

So i checked and the switch for on was not correct, fixed:

alias: Add Chlorine (cup input)
description: ''
trigger:
  - platform: state
    entity_id:
      - input_button.add_per_input_box_cup
    id: button
  - platform: time
    at: input_datetime.switch_off_time
    id: time
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: button
        sequence:
          - service: switch.turn_on
            data: {}
            target:
              entity_id: switch.pentair_chlor
          - service: input_datetime.set_datetime

Automation now triggers the correct switch, but did not turn off, so im assuming the datetime did not get set, but dont know how to check.

Just open developer tools → states and see what the entity state.

Did you change the switch name at the switch off part also?

Yes switch name changed in both places. In states, I just see the state turn from off to on, but then with a low number of cups, 0.148, which should equate to 20sec (0.148 / 1.66666666666667 / 16 = 0.00555 (0.000975 x 60 = 0.3329min > 20sec), the state does not change back… i waited a few mins but nothing changes.


I see the code has this:

  entity_id: input_datetime.switch_off_time

Should i have created a new entity?

I meant the datetime.

But I just realized an issue here.
Your calculation is to number of minutes, my calculation gives seconds.

So if your calculation should get you 20 seconds then that is 0 in my calculation.
I forgot you had minutes.

The datetime calculation should be:

              timestamp: >-
                {{ (((( states('input_number.cup_s') | float ) / 1.66666666666667
                / 16 ) * 60) + now().timestamp() ) | int }}

During my typing you posted:

Yes with both time and date.

ok, entity created and code changed, now i get on and then off, but very quick… should be 20 sec but i think its near instant off

Did you see the edit I made to the template?
You need to add * 60

But I’m not sure where the / 16 comes from?
You didn’t have that from the beginning.

Ok found the issue. the 16 is because the 1.666666 is gal/hr and im making the automation for cups now, so 16 cups in a gallon. The * 60 you added needed to be 3600 as im using hrs not mins.

Think i got it working now, thanks so much for your help.

Now if only i could fix the history_states to report decimal hours in more than 2 decimal places i would have everything in my project working. Any chance you know how to do that?

I don’t think you can change that.
The only workaround I can think of is to have a timer count while it’s on.
This will give you down to the second

That sounds like a good idea, now how to do that! Ill see what i can do