Automation variables in condition

My tasmotized wall plugs lose their connection to the MQTT broker on a regular basis.
So I tried to implement an automation to check if there are unavailable plugs and in case their was on, fire a restart command.
This works:

- alias: nousA1 restart
    id: nousa1_restart
      - platform: state
        to: "unavailable"
        # for: "00:03:00"
          - sensor.nousa101_power
          - sensor.nousa102_power
          - sensor.nousa103_power
          - sensor.nousa104_power
          - sensor.nousa105_power
          - sensor.nousa106_power
          - sensor.nousa107_power
      number: "{{trigger.entity_id[13:-6]}}"
    condition: []
      - service: shell_command.restart_tasmota #for power plugs
          number: "{{number}}"
    mode: parallel

unless I implement the condition:

condition: #[]
    # - or:
      # - condition: state
      #   entity_id: "binary_sensor.nousa1{{number}}_online"
      #   state: "on"
      - condition: template
        value_template: "{{ is_state('binary_sensor.nousa1{{number}}_online','on') }}"

This condition always renders false since the entity_id doesn’t get properly rendered (in dev tools’ templates too):

How do I get this working (other variable notation needed)?
Do variables even work in conditions?

The docs for automation and condition remain silent regarding the use of (own) variables.

Yes, but only in template conditions except in very limited circumstances which are always documented (one example is the for: parameter). Anywhere you can properly use a template, variables will work.

In your case here, it’s not working because the syntax of your template is incorrect. You are trying to use a template inside of a template instead of properly concatenating the entity_id string and variable.

This would be the correct syntax here.

- condition: template
  value_template: "{{ is_state('binary_sensor.nousa1' ~ number ~ '_online','on') }}"

Thank you very much. Works.
Would be great if that’d be written in the docs.
A pity I’m not educated enough to submit a PR to the docs.
(likewise: the shell_command docs don’t state that you have to restart HA for the entries to work; reloading the YAML isn’t enough).

Since it’s a basic function of Jinja templating, it’s in the Jinja docs (which are linked in the HA Templating docs and the Template editor tool):

The issue you were having was not with HA, it was with Jinja which is the “language” used in HA templates. While the HA documentation does have some helpful jinja hints and documents HA specific items, for basic jinja syntax you will need to refer to the jinja docs. Here’s a link to the operators section which explains the tilde (~) operator.

There is nothing to document. There is no reload service for shell_command. If there is no reload service for the integration in question, you must always restart HA for changes to take effect. You can check for (and run) a reload service in Dev Tools → Services. If there is no reload service, you must restart.

Thank you Drew and Jason.

It may be that the docs are “correct”, just specifying HA related stuff.
For me (and I’d bet more than half of the HA user base) it’s difficult to dig up the information I need.

First stop is always the forums, but the search doesn’t always yield what I (think to) need.

Next stop ist HA docs. Maybe they are technically correct and thorough, but of limited use for non-programmers if the crucial hints are missing.
Take the shell_command as an example: While your reason “If there is no reload service, you must restart.” makes perfect sense, it does so naturally only to the 10000 (power-)users who are fluent in all things HA. It isn’t at all obvious to the rest of us. That’s why this exact sentence is needed within the docs (“Remember: …!”) for the average Joe.

Last stop is the search engine of your choice. I must admit that for me this yields better results than the forum search (the first pages are nearly completely links to the forums). The HA docs links I don’t follow anymore - normally I’ve been there already.

As for the use of Jinja2, JS/TS and even YAML: some things you pick up scanning the examples and -even better- other users githubs. But it’s too much to ask of an average user to really learn these languages.
I’ve got a basic hang of YAML (and like it) since it’s only a descriptive language with limited functionality. Jinja and JavaScript on the other hand are real programming languages I never used before and I’ve got neither the brains nor the time to dive into them.

I only need examples which I can adapt to my needs. That is by far the main reason to search the forums and docs.
I’m sure most of the users see it this way.
And there is another important thing: spending hours of trial and error due to unobtainable pieces of information reduces the WAF to zero and below.

Sorry, that had to get out. It’s by no means aginst you two!