Get the state of an entity dynamically?

I can easily use different entities based on data passed into the script but I don’t seem to be able to get the state of those entities. For example, I call a script:

  - service: script.my_script
    data_template:
      my_number: 1

and this works and turns on switch.number1:

  - service: switch.turn_on
    data_template:
      entity_id: switch.number{{ my_number }}

but this doesn’t work, duration returns 00:0:00

  - service: timer.start
    data_template:
      entity_id: timer.my_timer
      duration: "00:{{ states('input_number.time{{ my_number }}') | int }}:00"

Is what I want to do even possible?

because you are using {{}} inside {{}}. {{}} tells jinja to execute this line as a return. I suggest brushing up on templating and Jinja:

http://jinja.pocoo.org/docs/2.10/

This may work:

  - service: timer.start
    data_template:
      entity_id: timer.my_timer
      duration: >
        {% set n = states('input_number.time' + my_number)  | int %}
          00:{{ '%02i' | format(n) }}:00

or

  - service: timer.start
    data_template:
      entity_id: timer.my_timer
      duration: "{{ '00:%02i:00' | format(states('input_number.time'+my_number) | int) }}"
2 Likes

That was really helpful, thank you.
The first suggestion worked and I have learned a new thing:-
set

That has moved me on in my learning but, I don’t understand why this (and other things I tried) doesn’t work (cycle is 1)?

  - wait_template: >
      {{ is_state('timer.cycle' + cycle + '_zone_duration', 'idle') }}

This does work.

  - wait_template: >
      {{ is_state('timer.cycle1_zone_duration', 'idle') }}

cycle is passed like this,

  - service: script.my_script:      
    data_template:
      cycle: 1

That’s probably because cycle is an integer, you cannot add integers and strings. This should work:

  - wait_template: >
      {{ is_state('timer.cycle' + cycle | string + '_zone_duration', 'idle') }}

you could also use format:

  - wait_template: >
      {{ is_state('timer.cycle%i_zone_duration' | format(cycle), 'idle') }}

That’s brilliant, thanks.

I was thinking along the right lines, that’s why I showed how I passed cycle.
I wondered if it were because it was an integer and I had even tried a couple of (made up :slight_smile: ) ways to convert it.

My main struggle is finding stuff documented. My first port of call is usually http://jinja.pocoo.org/docs/dev/templates/ but it’s far from perfect if you don’t already know what you’re looking for.

Anyway, thanks again!! I really appreciate it.

It takes some time learning API’s, especially because jinja isn’t documented heavily. If you familiarize yourself with python, jinja comes very naturally. It’s the only reason I know jinja.

Well…
I am afaid that didn’t work.
cycle is indeed a string already (I had forgotten it got passed along from a previous script - sorry about that but at least I learnt some new things).

So, I am still at a loss as to why

  - wait_template: >
      {{ is_state('timer.cycle' + cycle + '_zone_duration', 'idle') }}

doesn’t work? It sits there forever and I have confirmed with persistant notifications that timer.cycle1_zone_duration does change to ‘idle’ and cycle is 1

The full (abridged) chain is:

automation:
  action:
    - service: script.run_a_cycle
      data_template:
        cycle: 1


script:
  run_a_cycle:
    sequence:
          - service: script.irrigate_a_zone
            data_template:
              cycle: '{{ cycle }}'
              zone: 1

  irrigate_a_zone:
    sequence:
      - wait_template: >
          {{ is_state('timer.cycle' + cycle + '_zone_duration', 'idle') }}

Try this:

  - wait_template: >
      {% set entity = 'timer.cycle' + cycle + '_zone_duration' %}
      {{ is_state(entity , 'idle') }}

Also, look in your logs and check for errors or warnings. You may need to turn logging to debug.

1 Like

What can I say?
Thank you so much. That is perfect. I am sure I tried something along those lines when you showed me ‘set’ earlier on, but obviously I had something wrong.

Once again, thank you. I hope one day to be able to replay all the goodwill I have had from this forum since I joined a few weeks ago.

2 Likes