How can I make a physical button cycle through switches?

Hi

I have an analog patio heater which I control via IR/Broadlink.
The original remote has buttons for on/off and heat mode 1, 2, 3, 4 and 5.
I want to use a button where I cycle through these heat modes. If it starts on three, one press takes it to 4. If it starts on 5, one press takes in to 1. (It starts at the last setting).
I have created switches for 1-5. (switch.heat_mode_1, switch.heat_mode_2 etc)

But I don’t know how to create an automation or a scenario (or script?) that cycles through those on the press of a button. Any tips?

(Always starting at 1 is also and option (automation that powers on and executes heat_mode_1)

at its most basic
create a helper to store the current mode
have an automation - on button press, check helper - Set mode appropriately and on successful set - set helper to current mode. (or assume successful and just store the next mode.)

I’d probably use an input_select for the helper - it lets you guarantee what the modes are and provides a selection box if oyu want to include it in the UI.

Im not quite with you on the practical stuff.
Lets say I create a helper by the type “text” (i.e heat_mode_1).

How do I:

  1. Check the value of the helper, in an automation? (is the condition of a State?)
  2. Set a helper value from an automation (call service?)
  3. How do determine what the next heat mode is, based on the helper value?

I guess there’s templating in here somewhere, not my strong suit.

Any hints are appreciated, or a link to somewhere

As this is IR I’m guessing you don’t have state feedback from the heater?

If the heater can be operated manually (outside home assistant) it is not going to be possible to synchronise the state of the heater with the state stored in home assistant.

Thats correct, unless I only use HA to control it, and not the remote. I have visual feedback on the appliance, thats why it would be ok always starting from 1. I have read up on Helpers and have most thing figured out except how to ”add 1”, ie going from text_1 to text_2. A lot of If-Thens I guess

If the helper changes state, switch to the state it switched to.

Here is one I use to cycle thru fan speeds at the press of a button:

      - service: fan.set_percentage
        entity_id: fan.master_bedroom_fan
        data:
          percentage: >-
            {% if states('fan.master_bedroom_fan') == 'off' %}
              33
            {% elif states('fan.master_bedroom_fan') == 'on' and state_attr('fan.master_bedroom_fan', 'percentage') == 33 %}
              66
            {% elif states('fan.master_bedroom_fan') == 'on' and state_attr('fan.master_bedroom_fan', 'percentage') == 66 %}
              100
            {% elif states('fan.master_bedroom_fan') == 'on' and state_attr('fan.master_bedroom_fan', 'percentage') == 100 %}
              33
            {% endif %}

every button press sets the speed to the next higher level.

you could use that as a template to do what you want:

      - service: switch.turn_on
        entity_id: >
          {% if states('switch.switch_1') == 'off' and states('switch.switch_2')  == 'off' and states('switch.switch_3') == 'off'%}
            switch.switch_1
          {% elif states('switch.switch_1') == 'on' %}
            switch.switch_2
          {% elif states('switch.switch_2') == 'on' %}
            switch.switch_3
          {% elif states('switch.switch_3') == 'on' %}
            switch.switch_1
          {% endif %}

you will need to make a choose statement if you want to cycle all the switches off after switch_3 turns on.

1 Like

Hey Stefan, I am doing this to control four ceiling fans each with four speed settings using a Tradfri 5-button remote. I use the left/right to switch control between fans and the up/down to control the speed of the ‘current’ fan. It could be easily done using just a single Lovelace button if you’re controlling one heater.

I use counter helpers to keep track of the current fan and the current speed for each fan (0=off, 1=low, 2=medium, etc.).

The value of each counter is available as a state and can be changed via the counter increment/decrement/reset services. Based on your description you would probably only use the increment and reset i.e. press button, check counter value, if < 5 counter.increment else counter.reset.

I use a template to get the counter value and replace it with the name of the command for that particular fan speed that I have set up in the Broadlink IR remote. Then I call the Broadlink remote.send_command service to set the speed.

I am just about to start work for the day but can post some yaml later if you think it might help.

1 Like

Here is a simpler version of my automation, cut down to control one fan.

It has just dawned on me that if I had named my Broadlink remote commands as “1”, “2” etc. I wouldn’t need the template to convert from a number to a command name. Oh well, if it ain’t broke…

alias: Patio Fan control with counter helper
description: ''
trigger:
  - device_id: 3d3080f68b73d0aa54aa4459e6a3b1e7
    domain: zha
    platform: device
    type: remote_button_short_press
    subtype: dim_up
condition: []
action:
  - if:
      - condition: state
        entity_id: counter.1_tv_fan_speed
        state: '3'
    then:
      - service: counter.reset
        data: {}
        target:
          entity_id: counter.1_tv_fan_speed
    else:
      - service: counter.increment
        data: {}
        target:
          entity_id: counter.1_tv_fan_speed
  - service: remote.send_command
    data:
      device: tv fan
      command: >
        {{ states("counter.1_tv_fan_speed") | replace("0", "off") | replace("1",
        "low") | replace("2", "med") | replace("3", "high") }}
    target:
      entity_id: remote.broadlink_outside_tv_and_ceiling_fans
mode: single

Excellent, thanks