Change Switch state without performing action

configure this in your switch :

value_template: "false"

Thanks for the reply, how do I do this with a switchbot. I can’t see this as an allowable parameter and Check Configuration confirms that. - https://www.home-assistant.io/integrations/switchbot

  - platform: switchbot
    mac: 'FF:FF:FF:FF:FF:FF'
    name: 'Air Conditioner'

no idea :frowning: maybe you can create an extra template switch that controls your switch

I was miffed that the basic switch didn’t work like mqtt version which has separate mqtt command/set and state. It seems most basic “action/input” entities should support this as HA might be may be just a UI for an otherwise independent process controlled in various ways and by other UI’s. Since my code listens (via websocket api) for changes to this switch setting the switch was causing havoc looping. This script fixed all that and works great calling from via websocket api. I now have a new method for my api for changing just state just like with mqtt version. Thx!!

Well, after more investigation this script didn’t work. Changing the state (no matter in this script or not) triggers an event on the event bus which is picked up in my socket listener for that entity and causes a “double fire”. One wonders if you can disable the event bus temporarily during a state update? I saw some other posts where automations were turning off “homeassistant” then doing the switch state change then turning it back on. Only changing state of switch, no action I guess I’ll try that next.

for those who code in nodes and are interested here is my websocket api repo. It’s still a work in
progress and has no docs. https://git.kebler.net/UCOMmandIt/uci-ha.git

Gosh, I have exactly the same problem with my airco - what happens when someone triggers it manually. I’m trying to do this with an input boolean and automation in between

I think you need 3 things
Have your switchbot.airconditionner be used only for toggle. Not on/off
Create an input_boolean.airconditonner with on / off
Create another input_boolean.resetairconditonner

Then use automations 3 (!) to:

trigger: input_boolean.airconditonner change from off to on
condition “input_boolean.resetairconditonner” = off
action toggle switchbot.airconditionner

Then identical as above for " change from on to off" on the trigger

Finally automation to reset:
trigger “input_boolean.resetairconditonner” = on
no conditions
action: input_boolean.turn_off for airconditonner
delay 10 seconds
action: input_boolean.turn_off for resetairconditonner

I should have written that in code, but I am useless at writing it - it would take me half a day… :frowning:

In this case the trigger fires all the same

I have a Button Card to turn on a light (switch) via IR with a Broadlink Mini.

What I have so far is this:

  • tap: light turns on and off
  • hold: state sets to “on” without sending an IR signal
entity: switch.light_bedroom
show_icon: true
show_name: true
show_state: false
tap_action:
  action: toggle
hold_action:
  action: call-service
  service: python_script.set_statev2
  service_data:
    entity_id: switch.light_bedroom
    state: 'on'
type: button

The hold action calls the python_script.set_statev2 with the state of “on”
This works perfectly to update the state to on without sending that IR for when the physical light is on but home assistant it showing it as off.

What I’d like now it to send service_data conditionally based on the current state, something like :

...
  service_data:
    entity_id: switch.light_bedroom
    state: {{ 'off' if is_state('switch.light_bedroom','on') else 'off' }}
...

is this possible?

For anybody “newbie as I am” - here’s a write up on how to do this from scratch

In configuration.yaml add only this line

python_script:

Create a folder under “config” called “python_scripts”
It should end up like this: /config/python_scripts

Copy the script of Oligarch (on the right of the gray box, there is a copy symbol. Use that to make sure you all have the good identation, etc. You want to copy the box with “SCRIPT” in the top…

Open notepad, paste that text, and save it as “state_change.py” (and on a windows system, put the quotes around the filename otherwise you’ll end up with a .txt at the end and you don’t want that)

Restart Hassio / Home Assistant

Now you should be good to go to use it. It has created a new service you can call in automations, etc. The best place to test it out is
Developer tools > Services (tab at the top)

In there, the simple two lines of config seen below does the magic of changing the state of my switch called “switch.warmeroffice” to “off” without triggering the automation linked to that.

entity_id: switch.warmeroffice
state: 'off'

2 Likes

Thanks :slight_smile: Brilliant !!!

Thanks for the nice simple write up of a long thread. I needed to add the following line to this to make it work:

data_template:

So full code for the action in your example would be:

data_template:
  entity_id: switch.warmeroffice
  state: 'off'