Hi!!
I have an AC unit that has two REST endpoints:
1- get_state: returns a json of all its values (temperature, mode, fan, etc)
2- set_state: send a JSON with temperature, mode, fan, etc. values to set the ac state.
For the sake of simplicity, I’ll include only mode and fanspeed here. Here’s the essential part of my configuration:
rest_command:
silhouette_set_ac:
url: >-
{% set mode_mapper = {
'Auto': 0,
'Refroidissement': 1,
'Déshumidificateur': 3,
'Ventilateur': 4 } %}
{% set mode = mode_mapper[states('select.silhouette_state_mode')] %}
{% set fanspeed_mapper = {
'Auto': 0,
'Doux': 1,
'Moyen': 3,
'Fort': 4 } %}
{% set fanspeed = fanspeed_mapper[ states('select.silhouette_state_fanspeed')] %}
http://192.168.10.80/set_ac?mode={{ mode }}&fanspeed={{ fanspeed }}
rest:
- resource: http://192.168.10.80/get_ac
scan_interval: 5
verify_ssl: false
sensor:
- name: silhouette_state_mode
unique_id: silhouette_state_mode
value_template: >-
{% set mapper = {
0: 'Auto',
1: 'Refroidissement',
3: 'Déshumidificateur',
4: 'Ventilateur' } %}
{% set state = value_json.mode %}
{{ mapper[state] if state in mapper else 'Inconnu' }}
- name: silhouette_state_fanspeed
unique_id: silhouette_state_fanspeed
value_template: >-
{% set mapper = {
0: 'Auto',
1: 'Doux',
3: 'Moyen',
4: 'Fort' } %}
{% set state = value_json.fanspeed %}
{{ mapper[state] if state in mapper else 'Inconnu' }}
template:
select:
- name: silhouette_state_mode
unique_id: silhouette_state_mode
state: "{{ states('sensor.silhouette_state_mode') }}"
options: "{{['Auto', 'Refroidissement', 'Déshumidificateur', 'Ventilateur']}}"
select_option:
- service: rest_command.silhouette_set_ac
- name: silhouette_state_fanspeed
unique_id: silhouette_state_fanspeed
state: "{{ states('sensor.silhouette_state_fanspeed') }}"
options: "{{['Auto', 'Doux', 'Moyen', 'Fort']}}"
select_option:
- service: rest_command.silhouette_set_ac
Let’s pretend that currently, both selects are set to ‘Auto’. The selects update their values every 5 seconds. This is fine.
Now for example, when select.silhouette_state_mode
changes to something else, rest_command.silhouette_set_ac
gets called, BUT in there states('select.silhouette_state_mode')
is STILL ‘Auto’!
So I tried to pass the selected value like this for both selects:
template:
select:
- name: silhouette_state_mode
unique_id: silhouette_state_mode
state: "{{ states('sensor.silhouette_state_mode') }}"
options: "{{['Auto', 'Refroidissement', 'Déshumidificateur', 'Ventilateur']}}"
select_option:
- service: rest_command.silhouette_set_ac
data:
mode: "{{ option }}"
- name: silhouette_state_fanspeed
unique_id: silhouette_state_fanspeed
state: "{{ states('sensor.silhouette_state_fanspeed') }}"
options: "{{['Auto', 'Doux', 'Moyen', 'Fort']}}"
select_option:
- service: rest_command.silhouette_set_ac
data:
fanspeed: "{{ option }}"
And in the rest_command check for those variables:
silhouette_set_ac:
url: >-
{% set power = is_state('switch.silhouette_state_power', 'on') | abs %}
{% set mode_mapper = {
'Auto': 0,
'Refroidissement': 1,
'Déshumidificateur': 3,
'Ventilateur': 4 } %}
{% set mode = mode_mapper[mode if mode is defined else states('select.silhouette_state_mode')] %}
{% set fanspeed_mapper = {
'Auto': 0,
'Doux': 1,
'Moyen': 3,
'Fort': 4 } %}
{% set fanspeed = fanspeed_mapper[fanspeed if fanspeed is defined else states('select.silhouette_state_fanspeed')] %}
http://192.168.10.80/set_ac?power={{ power }}&mode={{ mode }}&fanspeed={{ fanspeed }}
With this, if I change the fan to ‘Doux’, the rest_command uses ‘Doux’ mode and ‘Auto’ fan. But then if I change the mode to ‘Ventilateur’, the rest_command sends ‘Auto’ fan and ‘Ventilateur’ mode.
So apparently, states()
always returns the state from its state template, NOT the selected value.
Now I could do a lot of things like using input_selects and mapping them to get_state and set_state apis with scripts and automations but wow is this getting out of hand! If there was a single REST endpoint for each value to set, I’d be done already but that’s not the case.
What’s the cleanest and simplest way to send the currently SELECTED values of multiple entities to a single rest api endpoint?