If I have template {% set value = 50 %}, how can I write that variable ‘value’ to input_number.anyname?
Something like this maybe? Can’t get it working
action:
- service: input.number_setvalue
metadata: {}
data:
value: “{{ value }}”
target:
entity_id: input_number.anyname
please use preformatted text bracketing to make your code readable for us.
your code block doesn’t show where you are defining the variable… I am presuming it is outside the block… Which means the scope of that variable {{ value}} doesn’t exist here
if you post the whole code including where it is defined and explain what your are trying to achieve (why you are doing this), we probably can find a workable solution…
1 Like
I’m making automation that does different calculations to Nordpool hour prices everytime Nordpool entity changes. Problem is that I haven’t found solution how to save those calculation for later use with different automations. Here’s simple version of what I want to do:
alias: Automation
description: ""
trigger:
- platform: state
entity_id:
- sensor.nordpool_kwh_fi_eur_0_150_024
condition: []
action:
- if:
- condition: template
value_template: >-
{% set prices = state_attr("sensor.nordpool_kwh_fi_eur_0_150_024",
"today")| list %}
{% set h = now().hour %}
{% if prices[h] < prices[h+1] %} {% set save = h+1 %} {% endif %}}
{{ prices[h] < prices[h+1] }}
then:
- service: input_number.set_value
metadata: {}
data:
value: "{{ save }}"
target:
entity_id: input_number.anyname
mode: single
Variables have local scope. In your example the variable save
has no known value in the then
clause of the if/then action.
Instead, you need to perform the calculations and assign the value to a variable at a wider scope so that it is available in both clauses.
alias: Automation
description: ""
trigger:
- platform: state
entity_id:
- sensor.nordpool_kwh_fi_eur_0_150_024
condition: []
action:
- variables:
prices: "{{ state_attr('sensor.nordpool_kwh_fi_eur_0_150_024', 'today')| list }}"
h: "{{ now().hour }}"
save: "{{ h + 1 }}"
- if:
- condition: template
value_template: '{{ prices[h] < prices[save] }}'
then:
- service: input_number.set_value
metadata: {}
data:
value: "{{ save }}"
target:
entity_id: input_number.anyname
mode: single
This may be a little off-topic, but you might want to check out the following custom macro:
This is familiar but not suitable of what I’m doing. That’s why I’m making my own
Ok, that make sense. Thank you!