How do I correctly use a variable in this automation?

I have spent an hour trying to get this to work, but I’m well and truly stuck. I am trying to use the info from two sensors when triggered. The trigger is any sensor.foobar_sone_x_tittel or _artist, and when triggered I want to send the state of that sensor (it’s a Scrape sensor) as a REST command.

alias: Sende beskjed om hva Foobar spiller i alle soner
description: ""
triggers:
  - trigger: state
    entity_id:
      - sensor.foobar_sone_1_tittel
      - sensor.foobar_sone_1_artist
      - sensor.foobar_sone_2_tittel
      - sensor.foobar_sone_2_artist
      - sensor.foobar_sone_3_tittel
      - sensor.foobar_sone_3_artist
      - sensor.foobar_sone_4_tittel
      - sensor.foobar_sone_4_artist
      - sensor.foobar_sone_5_tittel
      - sensor.foobar_sone_5_artist
      - sensor.foobar_sone_6_tittel
      - sensor.foobar_sone_6_artist
      - sensor.foobar_sone_7_tittel
      - sensor.foobar_sone_7_artist
      - sensor.foobar_sone_8_tittel
      - sensor.foobar_sone_8_artist
      - sensor.foobar_sone_9_tittel
      - sensor.foobar_sone_9_artist
conditions: []
actions:
  - variables:
      sone: |
        {{ trigger.entity_id | replace('sensor.foobar_sone_','') |
        replace('_tittel','') | replace('_artist','') }}
      artist: |
        {{ states('sensor.foobar_sone_{{ sone }}_artist') }}
      tittel: |
        {{ states('sensor.foobar_sone_{{ sone }}_tittel') }}
  - alias: Send til Girder
    data:
      kommandoklasse: Avspillingsinfo
      kommando: |
        {{ states('sensor.foobar_sone_ {{ sone }} _artist') }} {{ artist }} med {{ tittel }}
      sone: |
        {{ sone }}
    action: rest_command.girder

Seting the variable sone works, because I get the correct sone in the receiving end of the REST command. I have one extra entry for artist because I have tried both versions, setting it in the variable and setting it in the action. None of them work, I get three times “uknown” in the receiving end. I’m guessing it’s another one of those format errors, so can somebody please enlighten me?

You can’t nest template like {{ {{ }} }}. Instead use the variables directly or use them to build what you need. Strings can be concatenated using either ~ or +:

      tittel: |
        {% set ent = 'sensor.foobar_sone_' ~ sone ~ '_tittel' %}
        {{ states(ent) }}

FWIW, there are lots of tools to process strings so you don’t have to add so many replace filters. The following will return the first grouping of one or more digits:

      sone: |
        {{ trigger.entity_id | regex_findall("\d{1,}") | first | join }}
2 Likes

Thank you very much, that explains it! One more question: Sometimes the info for one of them is not there, and then I get unknown. Is it possible to add “med” at the end of the variable and then set the variable to ‘’ (so in essence nothing) when it’s unknown med?

I have tried several versions just to to add the “med” at the end of the artist variable, and I don’t even manage that…

“Med” is added, maybe I can manage this after all!

  artist1: |
    {% set ent = 'sensor.foobar_sone_'~ sone ~'_artist' %}
    {{ states(ent) }}
  artist2: "{{ artist1 }} med"
  artist: "{{ artist1 | replace('uknown med','') }}"
  tittel: |
    {% set ent = 'sensor.foobar_sone_'~ sone ~'_tittel' %}
    {{ states(ent) }}

And so it works. :grin:

I would probably do it something like:

  sone: |
    {{ trigger.entity_id | regex_findall("\d{1,}") | first | join }}
  artist: |
    {% set ent = 'sensor.foobar_sone_'~ sone ~'_artist' %}
    {{ states(ent)~' med' if has_value(ent) else '' }}
  tittel: |
    {% set ent = 'sensor.foobar_sone_'~ sone ~'_tittel' %}
    {{ states(ent) }}
1 Like

Thank you! That’s is for sure better!