A switch that triggers buttons in an endless loop

From my 3d printer I have two macros that Klipper esposes as buttons to Home Assistant:

  • Hepa Filter ON
  • Hepa Filter OFF

Additionally, I have a numeric sensor which maps the state of the pin where the Hepa Filter fan is connected: it goes from zero (off) to 255 (full speed, or whatever).

Thus one can change the status of the fan in several ways: 1- trigger one of the two macros 2- setting the value of the pin.

What I’m trying to achieve is to sum app these elements in a new switch with those behaviours:

1- When turned on, it push the button tied to the Filter On Macro
2- When turned off, it does the same for the Filter Off Macro
3- When one triggers one of the two macro from 3d printer WebUI, it updates its state accordingly
4- When one change the value of the pin from the 3d printer control panel, it updates its state (0 → off; >0 → on)

I create an input_boolean helper (do I need to definea switch in configuration files instead?)

The first two were easy to achieve with this automation:

alias: Qidi Tech - x-smart 3 - AC Filter Control
description: ""
trigger:
  - platform: state
    entity_id:
      - input_boolean.x_smart_3_filtro_ca
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: input_boolean.x_smart_3_filtro_ca
            state: "on"
        sequence:
          - device_id: 3fe374bea764c9f22220effbb43f221a
            domain: button
            entity_id: 0de8b8b4e0265f75c84754f4af7057b4
            type: press
      - conditions:
          - condition: state
            entity_id: input_boolean.x_smart_3_filtro_ca
            state: "off"
        sequence:
          - device_id: 3fe374bea764c9f22220effbb43f221a
            domain: button
            entity_id: 63652de4877b4ce5c4b71308262d6d9b
            type: press

For the remaining parts, The idea was to create another automation with three triggers and a conditional block against which trigger actually fired, to check what actually happened. Then use the switch.turn_on/off services on the helper as action.

…but apparently these services cannot be called from helpers :expressionless: I think I’m missing something here

Said that, I guess this would cause an endless loop. The toggle triggers the macro(s)…which actually update the switch…which triggers the macro(s)…nah, It cannot do the job this way I guess :confused:

what am i missing here? ?_?

Tnx allot, I’ll try this templae

…but how to solve the racing/endless condition loop? :open_mouth:

By selecting an appropriate value_template.

1 Like

Tnx allot, it was easier than expected using template switch :slight_smile:

- platform: template
  switches:
      qidi_tech_x_smart_3_carbon_filter:
        friendly_name: "Qidi Tech x-Smart 3 - Filtro CA"
        
        value_template: "{{ states('number.qidi_tech_x_smart_3_output_pin_fan2') | float > 0 }}"
        availability_template: >-
          {%- if not is_state("sensor.qidi_tech_x_smart_3_printer_state", "unavailable") %}
            true
          {% else %}
            false
          {%- endif %}
        icon_template: mdi:air-filter
        turn_on:
            service: button.press
            target:
              entity_id: button.qidi_tech_x_smart_3_macro_carbonfilter_on
            data: {}
        turn_off:
            service: button.press
            target:
              entity_id: button.qidi_tech_x_smart_3_macro_carbonfilter_off
            data: {}

no need for external automations or helpers at all :smiley:

1 Like

Nice. :+1:

You can simplify it a bit if you like:

        value_template: "{{ states('number.qidi_tech_x_smart_3_output_pin_fan2') | float(0) > 0 }}"
        availability_template: "{{ has_value('sensor.qidi_tech_x_smart_3_printer_state') }}"
1 Like

Ok this works as a charm…but I’ve some unwanted toggles when operating the switch.

I’ve seen that the sensor i use in value_template updates every 30 seconds only. Thus, when I operate the switch, it reverts to the previous state for 30 seconds, then it finally goes to the right state after that and stays there.

This is not fatal, I can live with that. But it’s somewhat annoying, indeed.

The polling frequency is something I can tune/control or shall I ask to who has created the integration to handle this (AFAIK this integration is just a wrapper for a REST api)?

You can fix that by updating the entity the value_template relies on whenever the switch is operated:

- platform: template
  switches:
      qidi_tech_x_smart_3_carbon_filter:
        friendly_name: "Qidi Tech x-Smart 3 - Filtro CA"
        value_template: "{{ states('number.qidi_tech_x_smart_3_output_pin_fan2') | float(0) > 0 }}"
        availability_template: "{{ has_value('sensor.qidi_tech_x_smart_3_printer_state') }}"
        icon_template: mdi:air-filter
        turn_on:
          - service: button.press
            target:
              entity_id: button.qidi_tech_x_smart_3_macro_carbonfilter_on
            data: {}
          - delay: 1 # may not be needed or may need to be increased
          - service: homeassistant.update_entity
            entity_id: number.qidi_tech_x_smart_3_output_pin_fan2
        turn_off:
          - service: button.press
            target:
              entity_id: button.qidi_tech_x_smart_3_macro_carbonfilter_off
            data: {}
          - delay: 1 # may not be needed or may need to be increased
          - service: homeassistant.update_entity
            entity_id: number.qidi_tech_x_smart_3_output_pin_fan2

Test without the delays first. If needed add them in and increase them until you get a reliable switch state change without bouncing.

1 Like