Update input_number based json values

I’m looking for a automation way to dynamically set an input_number based on the values of the following…

{“DS18B20-1”:{“Id”:“00000BEC9A82”,“Temperature”:82.6},
“DS18B20-2”:{“Id”:“00000BECB477”,“Temperature”:76.4},
“DS18B20-3”:{“Id”:“00000BECCA98”,“Temperature”:80.9}

I have 3 mqtt sensors and 3 input_numbers and 3 template sensors…

sensor.[ t1…t3 ]_sensor ( represents mqtt sensor )
input_number.[ t1…t3 ]
sensor.[ t1…t3 ] ( template sensor )

Need to update input number based on the sorted temps ( low to high ) triggerd by a specific topic

Example:

  • trigger: mqtt listening to my/topic

  • sort the temperatures

  • input_number.t1 = 2
    input_number.t2 = 3
    input_number.t3 = 1

Display temps in template sensor

{% set t1_num = states('input_number.t1') %}
{{ states('sensor.t' + t1_num) }}

{% set t2_num = states('input_number.t2') %}
{{ states('sensor.t' + t2_num) }}

{% set t3_num = states('input_number.t3') %}
{{ states('sensor.t' + t3_num) }}

Looking forward to hearing a few solutions!

Thanks

The setting of the input_number needs to be done with a service call. And if you place the sensors in an array you can sort them with the sort filter. For the value of the lowest input_number's would be

{% set nums = [states('input_number.t1')|float] %}
{% set nums = nums + [states('input_number.t2')|float] %}
{% set nums = nums + [states('input_number.t3')|float] %}
{{ (nums|sort)[0] }}

@septillion I left the service call part out as I thought that would be understood.

As far as your recommendation… I need to map the sorted mqtt sensors to the input_numbers.

Like this…

“DS18B20-1”:“Temperature”:82.6 would set input_num.t3 = 1

“DS18B20-2”:"Temperature”:76.4 would set input_num.t1 = 2

“DS18B20-3”:"Temperature”:80.9 would set input_num.t2 = 3

Notice how the input_num equals the index of the temp sensor…

input_num.t3 = 1 which maps to the temp sensor DS18B20-1 ( hightest temp )
input_num.t1 = 2 which maps to the temp sensor DS18B20-2 ( lowest temp )
input_num.t2 = 3 which maps to the temp sensor DS18B20-3 ( middle temp )

Mm, that’s a bit harder. I can only think about the ugly solution to now check to which sensor the now sorted value came from.

{% set nums = [states('input_number.t1')|float] %}
{% set nums = nums + [states('input_number.t2')|float] %}
{% set nums = nums + [states('input_number.t3')|float] %}
{% for n in nums %}
{% if n == (nums|sort)[1] %}
{{ loop.index }}
{% endif %}
{% endfor %}

@septillion
Something like this. Use the index to set the input_number. Question is how? I suspect it can’t be done in an automation ( or can it? ), so with a python_script?

{% set t1 = 83 %}
{% set t2 = 76 %}
{% set t3 = 81 %}

{% set temps = [t1,t2,t3] %}
{% set temps_sorted = temps | sort %}

temps: {{ temps }}
temps_sorted: {{ temps_sorted }}
{% for temp in temps_sorted %}
{{ temps.index(temp) }}
{% endfor %}

EDIT: adding template output

temps: [83, 76, 81]
temps_sorted: [76, 81, 83]

1

2

0

Now you simply need to make an automation in which you do three input_number.set calls. You will not be able to do so in a single call.

Alternatively, you could do all this in three template sensors (or a single one with a list). But you would have to break this again when you want to use it to select a sensor.

Or as alternative to the alternative, switch to an input_text to store a list.