Help with service_template (SOLVED)

Make a template switch

i follow the instructions but for some reason the template switch doesnt sync with the device service switch (climate.turn_on / climate.turn_off)

- platform: template
  switches:

  dehumidifier_1:
    entity_id: climate.midea_dehumi_1
    friendly_name: Dehumidifier
    value_template: "{{ is_state('climate.midea_dehumi_1', 'off') }}"

    turn_on:
      service: climate.turn_on
      data:
        entity_id: climate.midea_dehumi_1
    turn_off:
      service: climate.turn_off
      data:
        entity_id: climate.midea_dehumi_1

change to ‘on’

doesnt solve the problem

any errors in the logs? homeassistant.log, not the gui

probably won’t solve it either, and I don’t have a climate switch, but why don’t you try:

  dehumidifier_1:
    friendly_name: Dehumidifier
    value_template: > 
       {{ is_state('climate.midea_dehumi_1', 'on') }}
    turn_on:
      service: climate.turn_on
      entity_id: climate.midea_dehumi_1
    turn_off:
      service: climate.turn_off
      entity_id: climate.midea_dehumi_1

you have an entity_id listed under the switch name, which is not documented Template Switch - Home Assistant

also, when no extra data is needed in the service, you can leave the data field out… (which is contrary to the doc)

Ok… this entity relies on this github ( https://github.com/barban-dev/midea_inventor_dehumidifier )
and hassio states it’s an untested component by the hassio team (warning) so i know that there may be issues implementing it.

I’ve tried every possible solution coding but i get this:

  • when device is off (while this switch is off) the switch works and it turns the dehumidifier on
  • when device is on (while the switch is on) the switch doesnt work, it doesnt turn the dehumidifier off and the switch goes from on to off
  • the switch doesnt sync automatically with the state of the device. If the device is on when opening hassio (from web or the app) the switch appears off

can you switch it off via the dev-service page?

please show us the various states in the dev-state page by screenshot, so we can see what state and attributes the entity has.

I can switch it on or off calling the respective services for the entity in the Dev service section (climate.turn_on or climate.turn_off services for the entity)
or even with this code in ui-lovelace.yaml (in a card):

      - type: entities
        show_header_toggle: false
        entities:              
          - type: call-service
            icon: mdi:power
            name: Dehumiditifier
            action_name: Off
            service: climate.turn_off
            entity_id: climate.midea_dehumi_1
          - type: call-service
          ..... same code for climate.turn_on

and they work

Well that says it all… you have to check for the state . So ‘target_mode-ion_on’ should be in the value_ template

doesnt the value_template return true or false which equals on or off for the switch?

your value template evaluates the state. And checks it to be the ‘on’ state. Normally, with switches, lights, input_booelans, etc etc, that would be either ‘on’ or ‘off’. As @petro suggested before.
Hence the
{{ is_state('climate.midea_dehumi_1', 'on') }}

AS you’ve said, it doesn’t follow the true state of the climate entity. Thats why I asked you to show the dev-state page.

Your screenshot clearly shows the state to be ‘target_mode-ion_off’ which I presume to be the state for ‘off’. Since we want to know the state for ‘on’ I take it it would be ‘target_mode-ion_on’

Did you give it a try?

  dehumidifier_1:
    friendly_name: Dehumidifier
    value_template: > 
       {{ is_state('climate.midea_dehumi_1', 'target_mode-ion_on') }}
    turn_on:
      service: climate.turn_on
      entity_id: climate.midea_dehumi_1
    turn_off:
      service: climate.turn_off
      entity_id: climate.midea_dehumi_1

the turn_on and turn_off commands are fine, as you confirm, its just the state being evaluated wasn’t the right one…

yes, you’re absolutely right.
I’ve realised that 10minutes prior to your reply.
It seems that the switch needs to evaluate correctly the value_template is_state otherwise it wont work properly.
For that reason, instead of “on” i need to check if the device is set on one of the following states:
“target_mode-ion_on”,
“continuos_mode-ion_on”,
“smart_mode-ion_on”,
“dryer_mode-ion_on”,
“target_mode-ion_off”,
“continuos_mode-ion_off”,
“smart_mode-ion_off”,
“dryer_mode-ion_off”

I tried it with target_mode-ion_on and the switch worked just fine turning the device on and off

ok once the issue was identified it was a matter of time…
here’s the working code… finally:

  - platform: template
    switches:
      dehumidifier_1:
      friendly_name: Dehumidifier
      value_template: >-
        {% if is_state("climate.midea_dehumi_1", "target_mode-ion_on") or
                is_state("climate.midea_dehumi_1", "target_mode-ion_off") or
                is_state('climate.midea_dehumi_1', 'smart_mode-ion_on') or
                is_state('climate.midea_dehumi_1', 'smart_mode-ion_off') or
                is_state('climate.midea_dehumi_1', 'continuos_mode-ion_on') or
                is_state('climate.midea_dehumi_1', 'continuos_mode-ion_off') or 
                is_state('climate.midea_dehumi_1', 'dryer_mode-ion_on') or
                is_state('climate.midea_dehumi_1', 'target_mode-ion_off')%}
            True
        {% else %}
            False
        {% endif %}

for anyone else with the same dehumidifier

make that:

 value_template: >-
    {{ is_state("climate.midea_dehumi_1", "target_mode-ion_on") or
       is_state("climate.midea_dehumi_1", "target_mode-ion_off") or
       is_state('climate.midea_dehumi_1', 'smart_mode-ion_on') or
       is_state('climate.midea_dehumi_1', 'smart_mode-ion_off') or
       is_state('climate.midea_dehumi_1', 'continuos_mode-ion_on') or
       is_state('climate.midea_dehumi_1', 'continuos_mode-ion_off') or 
       is_state('climate.midea_dehumi_1', 'dryer_mode-ion_on') or
       is_state('climate.midea_dehumi_1', 'target_mode-ion_off') }}

or:

value_template: >
    {{ states('climate.midea_dehumi_1') in 
        ['target_mode-ion_on', 'target_mode-ion_off', 'smart_mode-ion_on','smart_mode-ion_off','continuos_mode-ion_on',
          'continuos_mode-ion_off','dryer_mode-ion_on', 'target_mode-ion_off'] }}
1 Like

tried it, replacing the " with ’
but it doesnt pass the validation code

not really sure what you are saying here? please post the code you tried?

your first suggestion didn’t pass the validation code
but your second suggestion with the table not only is it brilliant and clean but it passed validation and it works.
Thank you

1 Like

cool, glad to be of help. enjoy!

1 Like

Hi all, i have a simple Aqara one-button switch, and I just want to use a simple code: if state of the light is on, then turn it off, else turn it on.

I read 100 forums but I can’t get it to work. What is wrong with my code?

- id: '1576885798868'
  alias: Kitchen Switch ON-OFF
  description: ''
  trigger:
  - event_data:
      event: 1002
      id: switch_kitchen
    event_type: deconz_event
    platform: event
  condition: []
  action:
  - service_template: >
      {% if is_state('light.kitchen_lights'), 'on' %}
        script.kitchen_lights_off
      {% else %}
        script.kitchen_lights_on
      {% endif %}

Note that the two scripts are working fine. If I trigger them independently, the lights turn on and off, as expected. The trigger also works fine. The logs show the automation triggered.