Trying to use a variable passed to a script as a state trigger within the script

Hey all,

I’ve got a working automation that forcibly discharges my home battery down to certain thresholds at various points in the day. Eg. at 20:30 it’ll discharge down to 50%, and at 21:30 it’ll discharge to 25% and so on. It look like this:

alias: "Libbi: Timed exports"
description: ""
trigger:
  - platform: time
    at: "20:30:00"
    id: "2030"
  - platform: time
    at: "21:30:00"
    id: "2130"
  - platform: time
    at: "22:30:00"
    id: "2230"
  - platform: time
    at: "23:00:00"
    id: "2300"
condition:
  - condition: not
    conditions:
      - condition: state
        entity_id: sensor.myenergi_zappi_22100218_status
        state: Boosting
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - "2030"
          - condition: numeric_state
            entity_id: sensor.myenergi_libbi_24049368_soc
            above: 50
        sequence:
          - service: notify.telegram_us
            data:
              data:
                message_tag: libbi
              message: >-
                Libbi at {{ states('sensor.myenergi_libbi_24049368_soc') }}%
                charge. Exporting to 25%
          - if:
              - condition: state
                entity_id: sensor.myenergi_zappi_22100218_plug_status
                state: EV Connected
            then:
              - device_id: 29e9524df6815f8f22c8b154295dcf8d
                domain: select
                entity_id: 201e81484bc3aa7f614de6916ddc9c80
                type: select_option
                option: Stopped
          - device_id: 8c1f789db93bb94e82f47e9fda5ba4f1
            domain: select
            entity_id: efbdc28be6e6c52562e33d7d4492fa1f
            type: select_option
            option: Export
          - wait_for_trigger:
              - platform: state
                entity_id:
                  - sensor.myenergi_libbi_24049368_soc
                to: "25"
            timeout:
              hours: 0
              minutes: 59
              seconds: 30
              milliseconds: 0
            continue_on_timeout: false
          - device_id: 8c1f789db93bb94e82f47e9fda5ba4f1
            domain: select
            entity_id: efbdc28be6e6c52562e33d7d4492fa1f
            type: select_option
            option: Normal
          - service: notify.telegram_us
            data:
              data:
                message_tag: libbi
              message: Export stopped.
          - if:
              - condition: state
                entity_id: select.myenergi_zappi_22100218_charge_mode
                state: Stopped
            then:
              - device_id: 29e9524df6815f8f22c8b154295dcf8d
                domain: select
                entity_id: 201e81484bc3aa7f614de6916ddc9c80
                type: select_option
                option: Eco+

… And then there’s 3 more options that do the exact same sequence just with the different time and SoC conditions. It works but it’s obviously super clunky, so I’ve been trying to convert that sequence into a script in order to simplify it. So now, for the first block, I’m just doing this:

action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - "2030"
          - condition: numeric_state
            entity_id: sensor.myenergi_libbi_24049368_soc
            above: 50
        sequence:
          - service: script.libbi_export
            data:
              target: "50"

And then my script contains the main sequence:

alias: Libbi Export
sequence:
  - service: notify.telegram_me
    data:
      data:
        message_tag: libbi
      message: >-
        Libbi at {{ states('sensor.myenergi_libbi_24049368_soc') }}% charge.
        Exporting to {{ target }}%
  - if:
      - condition: state
        entity_id: sensor.myenergi_zappi_22100218_plug_status
        state: EV Connected
    then:
      - device_id: 29e9524df6815f8f22c8b154295dcf8d
        domain: select
        entity_id: 201e81484bc3aa7f614de6916ddc9c80
        type: select_option
        option: Stopped
  - device_id: 8c1f789db93bb94e82f47e9fda5ba4f1
    domain: select
    entity_id: efbdc28be6e6c52562e33d7d4492fa1f
    type: select_option
    option: Export
  - wait_for_trigger:
      - platform: state
        entity_id:
          - sensor.myenergi_libbi_24049368_soc
        to: "{{ target }}"
    timeout:
      hours: 0
      minutes: 59
      seconds: 30
      milliseconds: 0
    continue_on_timeout: false
  - device_id: 8c1f789db93bb94e82f47e9fda5ba4f1
    domain: select
    entity_id: efbdc28be6e6c52562e33d7d4492fa1f
    type: select_option
    option: Normal
  - service: notify.telegram_me
    data:
      data:
        message_tag: libbi
      message: Export stopped.
  - if:
      - condition: state
        entity_id: select.myenergi_zappi_22100218_charge_mode
        state: Stopped
    then:
      - device_id: 29e9524df6815f8f22c8b154295dcf8d
        domain: select
        entity_id: 201e81484bc3aa7f614de6916ddc9c80
        type: select_option
        option: Eco+
mode: single
icon: mdi:battery-arrow-down

Where I’m coming unstuck is that the {{ target }} variable works great inside the notification message, but I can’t make it work inside the wait_for_trigger action.

After a lot of Googling I’ve also tried doing it as a wait_template in various guises but none of these work either:

  - wait_template: "{{ is_state('sensor.myenergi_libbi_24049368_soc','{{ target }}') }}"
    timeout: "00:59:30"
    continue_on_timeout: false

and (with and without single quotes around target):

  - wait_template: "{{ is_state('sensor.myenergi_libbi_24049368_soc',target) }}"
    timeout: "00:59:30"
    continue_on_timeout: false

Very much clutching at straws, but as this is reporting battery level, perhaps I need to include the % sign, but I can’t make that work either.

I feel like this must be possible but I’m pulling my hair out trying to get the syntax right! After what feels like a wasted afternoon I’m very much open to any suggestions here.

Thanks everyone.

A wait_for_trigger’s inability to handle script variables appears to be a bug.

If you’re experiencing the same problem with wait_template then it may have the same bug.

FWIW, this template you tried would never work because you cannot nest templates like that.

wait_template: "{{ is_state('sensor.myenergi_libbi_24049368_soc','{{ target }}') }}"

The second thing you tried was correct as far as templating goes but if it failed to work then it implies wait_template has the same issue with script variables as wait_for_trigger.

wait_template: "{{ is_state('sensor.myenergi_libbi_24049368_soc',target) }}"

BTW, when choosing a variable name, try to avoid a name that is identical to a scripting statement (i.e. like “target”).

Ha, well that explains a lot! I tried changing the variable to a more suitable name just in case it was tripping up the wait_template (thanks for that tip), but it didn’t make any difference.

I also tried setting the value of an input_number helper at the start of the script:

  - service: input_number.set_value
    metadata: {}
    data:
      value: "{{ export_target }}"
    target:
      entity_id: input_number.libbi_export_target

And using that as the trigger either in the wait_for_trigger:

  - wait_for_trigger:
      - platform: state
        entity_id:
          - sensor.myenergi_libbi_24049368_soc
        to: "{{ states('input_number.libbi_export_target') }}"
    timeout: "00:59:30"
    continue_on_timeout: false

Or the wait_template:

  - wait_template: "{{ is_state('sensor.myenergi_libbi_24049368_soc',input_number.libbi_export_target) }}"
    timeout: "00:59:30"
    continue_on_timeout: false

But neither seems to work (though I’m not sure I’m doing the wait_template correctly as the script immediately goes to current: 0 in the States listing but it doesn’t continue with the rest of the actions).

I’ll subscribe to the thread you linked anyway. It’s interesting that it’s a 2 year old stale bug that suddenly got traction again only 6 days ago!

Thanks very much for your help.