Variable in entity_id

Why is this not working? (asked by absolute beginner)

alias: Test
description: “”
triggers:
conditions:
actions:

  • action: input_number.set_value
    metadata: {}
    data:
    value: |
    {% set entity = “input_number.ps_cpu” %}
    {% set waardeN = 24 %}
    {{ waardeN }}
    target:
    entity_id: “{{ entity }}”
    mode: single

You defined the Jinja2 variable named entity within the action’s value option. That means entity can only be referenced within the value option. It is undefined elsewhere.

That’s why your action fails because the entity variable is referenced in a different option, the entity_id option.

The Jinja2 variable named waardeN works because it is defined and referenced within the same option (value).

The entity variable will work if you define it within the entity_id option or if you define it as a script variable.

actions:
  - variables:
      entity: input_number.ps_cpu
  - action: input_number.set_value
    metadata: {}
    data:
      value: |
        {% set waardeN = 24 %}
        {{ waardeN }}
    target:
      entity_id: "{{ entity }}"

I think I understand.
But in my mind it still leaves the question, can I do this to change the entity?
Because it is only in the value statement that i know the required entity.

actions:

  • variables:

    entity: input_number.ps_cpu

  • action: input_number.set_value
    metadata: {}
    data:
    value: |
    {% set entity = “input_number.ps_temp” %}
    {% set waardeN = 24 %}
    {{ waardeN }}
    target:
    entity_id: “{{ entity }}”

I don’t understand how you are limited to knowing the required entity to just within the value option.

Why is the entity unknown before the action?

Thanks.
The whole automation is triggered with MQTT.
The MQTT contents reports a raspberry pi status. (CPU, Temp. SSID etc.)
I examine the input within the value statement and that sets a helper to show the info.

Please post the complete automation (as properly formatted YAML).

The essential part looks like this. And thanks for your help.

alias: Test
description: ""
triggers:
  - topic: MQTT/PrintServer/#
    trigger: mqtt
conditions: []
actions:
  - variables:
      rcvd: "{{ trigger.topic.split('/') }}"
      load: "{{ trigger.payload }}"
  - action: input_number.set_value
    metadata: {}
    data:
      value: |
        {% set device = rcvd[1] %}
        {% set action = rcvd[2] %}
        {% set value = load %}
        {% if action == "CPU" %}
          {% set entity = "input_number.ps_cpu" %}
          {% set waardeN = value %}
        {% endif %}
        {% if action == "Temp" %}
          {% set entity = "input_number.ps_temp" %}
          {% set waardeN = value %}
        {% endif %}
        {{ waardeN }}
    target:
      entity_id: {{ entity }}
mode: single
alias: Test
description: ""
triggers:
  - topic: MQTT/PrintServer/#
    trigger: mqtt
conditions: []
actions:
  - variables:
      rcvd: "{{ trigger.topic.split('/') }}"
      load: "{{ trigger.payload }}"
      action: "{{ rcvd[2] }}"
      entity: "{{ 'input_number.ps_' ~ action | lower }}"
  - action: input_number.set_value
    metadata: {}
    data:
      value: "{{ load }}"
    target:
      entity_id: "{{ entity }}"
mode: single

Thanks for posting the complete automation. Now I understand what you are trying to do.

In fact, it doesn’t need script variables and can be done like this:

alias: Test
description: ""
triggers:
  - trigger: mqtt
    topic: MQTT/PrintServer/#
conditions: []
actions:
  - action: input_number.set_value
    metadata: {}
    target:
      entity_id: "input_number.ps_{{ trigger.topic.split('/')[-1] | lower }}"
    data:
      value: "{{ trigger.payload }}"
mode: single

NOTE

Is there a particular reason you are storing the received values into Input Numbers? Is it because you want to ensure the values are not lost after restarting Home Assistant?

Because if that’s the reason then my preference would be to store them in a Trigger-based Template Sensor. This type of entity doesn’t lose its value after a restart.

template:
  - trigger:
      - trigger: mqtt
        topic: MQTT/PrintServer/CPU
    sensor:
      - name: CPU Usage
        state: "{{ trigger.payload }}"
        unit_of_measurement: '%'

  - trigger:
      - trigger: mqtt
        topic: MQTT/PrintServer/Temp
    sensor:
      - name: CPU Temperature
        state: "{{ trigger.payload }}"
        unit_of_measurement: '°C'

In addition, by supplying the unit_of_measurement option, each sensor’s values will be automatically graphed by Home Assistant.

For example:

Another alternative is to model it as an MQTT Sensor but this kind of sensor will lose its value after a restart if the payload is not published as a retained message.

If the payload is published as a retained message then it’s simpler to use MQTT Sensor instead of Trigger-based Template Sensor.

mqtt:
  sensor:
    - name: CPU Usage
      state_topic: MQTT/PrintServer/CPU
      unit_of_measurement: '%'
    - name: CPU Temperature
      state_topic: MQTT/PrintServer/Temp
      unit_of_measurement: '°C'

Taras and Rick,
Thanks you so much. You’ve given me a lot to think about.
I have learned a great deal and I would never have come to these insights.
Wim

You’re welcome!

Unless there is some special reason to store the values in Input Numbers, I recommend you consider using what I suggested in my previous post. Use either a Trigger-based Template Sensor or simply an MQTT Sensor (if the payload is published as a retained message).

Thanks to your help I achieved everything I wanted. Since the RPi’s are mine, I changed their MQTT reporting to use retain=True and used the MQTT Sensor method.
I have a remaining question about the graph in one of the previous posts. How can I achieve such a graph on my dashboard?
Wim

More specifcally, how to make a simular graph for the on/off status of a switch?

Glad to hear one of my suggestions made it easier to achieve your requirements. Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which indicates to others that the topic has been solved. It also puts a link at the top of the topic that leads to tbe Solution post. This helps other users find answers to similar questions.

I suggest you consider using the History Graph card.

I suggest the History Graph card.

Sorry, dumb question.
Wim