Select-option with variable

Hi
I can’t pass a variable into an automation that uses select_option
Automatisation

alias: Test Tempo HP Blanc
description: ""
triggers:
 - trigger: state
   entity_id:
     - sensor.electricite_teleinfo_tarif_en_cours_texte
   variables:
     tariff: Blanc HP
conditions:
 - condition: state
   entity_id: sensor.electricite_teleinfo_tarif_en_cours_texte
   state: "9"
actions:
 - target:
     entity_id: select.electricite_pompe_puisard_energie_jour
   data:
     option: "{{ tariff }}"
   action: select.select_option
mode: single

Utility_meter

###############################################
############### UTILITY METERS #################
################################################
utility_meter:
electricite_pompe_puisard_energie_jour:
source: sensor.electricite_energie_pompe_puisard_index_total_kwh
name: Electricite pompe_puisard Energie Jour
cycle: daily
tariffs:
- Bleu HP
- Bleu HC
- Blanc HP
- Blanc HC
- Rouge HP
- Rouge HC

Template

 {% set d = {
         '0': 'Base',
         '1': 'HC',
         '2': 'HP',
         '5': 'Bleu HC',
         '6': 'Blanc HC',
         '7': 'Rouge HC',
         '8': 'Bleu HP',
         '9': 'Blanc HP',
         '10': 'Rouge HP'} %}
       {{ d.get( states('sensor.electricite_teleinfo_tarif_en_cours') ) }}

knx_sensor

 - name: electricite_teleinfo_tarif_en_cours
   state_address: "9/7/255"
   sync_state: init
   type: tariff

Error message when running automation

If I replace {{ tariff }} with Blanc HP it works

alias: Test Tempo HP Blanc
description: ""
triggers:
 - trigger: state
   entity_id:
     - sensor.electricite_teleinfo_tarif_en_cours_texte
   variables:
     tariff: Blanc HP
conditions:
 - condition: state
   entity_id: sensor.electricite_teleinfo_tarif_en_cours_texte
   state: "9"
actions:
 - target:
     entity_id: select.electricite_pompe_puisard_energie_jour
   data:
     option: Blanc HP
   action: select.select_option
mode: single

This solution requires many lines of code for all entities and I would like to use a variable.
Initially I used the following code which worked for several months

target:
      entity_id: >
        {{ states.select | selectattr('name','search','energie') |
        map(attribute='entity_id') | list }}
    action: select.select_option

Afaik you can use a variable, and the debug output suggests the template is expanded. The problem is that you did not set a variable named tariff, so what is passed to the service is empty.

Where should the variable come from? is the value contained in an entity? If so, you do not need a variable, you can get the state from the entity.

But your question isn’t very clear, because wht you had was a template for an entity id, and what you were trying was a template for an option.

113 / 5 000

Résultats de traduction

My apologies, after many attempts, I did not copy/paste the correct version of the automation code.

alias: Test Tempo HP Blanc
description: ""
triggers:
  - trigger: state
    entity_id:
      - sensor.electricite_teleinfo_tarif_en_cours_texte
    variables:
      tariff: Blanc HP
conditions:
  - condition: state
    entity_id: sensor.electricite_teleinfo_tarif_en_cours_texte
    state: "9"
actions:
  - target:
      entity_id: select.electricite_pompe_puisard_energie_jour
    data:
      option: Blanc HP
    action: select.select_option
  - target:
      entity_id: select.electricite_pompe_puisard_energie_semaine
    data:
      option: " {{ tariff }}"
    action: select.select_option
mode: single

with error message after execution

I’m not sure, but I think the variable exists only in the trigger. But for these cases, many use the trigger id to relay information to the action block:

alias: Test Tempo HP Blanc
description: ""
triggers:
  - trigger: state
    entity_id:
      - sensor.electricite_teleinfo_tarif_en_cours_texte
    id: "Blanc HP"
conditions:
  - condition: state
    entity_id: sensor.electricite_teleinfo_tarif_en_cours_texte
    state: "9"
actions:
  - target:
      entity_id: select.electricite_pompe_puisard_energie_jour
    data:
      option: Blanc HP
    action: select.select_option
  - target:
      entity_id: select.electricite_pompe_puisard_energie_semaine
    data:
      option: " {{ trigger.id }}"
    action: select.select_option
mode: single

Looking at the flow diagram of the trace you shared it appears that you are using the “Run” function. When doing that, or using the automation.trigger action, the trigger and condition blocks are skipped… so the trigger-attached variable you have set up is not instantiated and holds no value. The automation looks fine, it’s your testing procedure that is letting you down.

Finally, with the help of ChatGPT I arrived at a code that works for all the entities involved.

- variables:
      option: '{{ states(''sensor.electricite_teleinfo_tarif_en_cours_texte'') }}'
      utility_meters: >
        {{ states | selectattr('entity_id', 'search',
        '_energie_(jour|semaine|mois|annee)$') | map(attribute='entity_id') |
        list }}
  - repeat:
      for_each: '{{ utility_meters }}'
      sequence:
        - data:
            entity_id: '{{ repeat.item }}'
            option: '{{ option }}'
          action: select.select_option