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 }}”
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.
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.
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
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
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.