Resend switch state on hold_action:(solved)

Tags: #<Tag:0x00007f739b711090>

So i have a button like below if i press and hold it it will resend switch.turnoff even if the state is already off, is there any way to get it to resend on when its on and off when its off??

so what i’m looking for is: change “service: switch.turn_off” to “service: switch.turn_on” depending on the state of the switch
i cant figure out of its even possible, anyone have any ideas?

type: button
tap_action:
  action: toggle
hold_action:
  action: call-service
  service: switch.turn_off
  service_data:
    entity_id: switch.window
show_icon: true
show_name: true
show_state: false
entity: switch.window

Hey stefan,

should be pretty easy.
Create a new script and call it from the button.
Inside the script, use templating to determine the service to call: https://www.home-assistant.io/docs/scripts/service-calls/#use-templates-to-decide-which-service-to-call

1 Like

Ok so template script, i never got that to work, but it gave me a starting point for my searches and i found this: https://community.home-assistant.io/t/how-do-you-use-service-template-when-different-services-use-different-attributes-solved/52314

So a BIG thank you for pointing me in the right direction

i modified it like this

entity_id = data.get('entity_id')
statee = hass.states.get(entity_id).state

if  statee == 'off':
    service_data = {'entity_id': entity_id }
    hass.services.call('switch', 'turn_off', service_data)
else:
    service_data = {'entity_id': entity_id }
    hass.services.call('switch', 'turn_on', service_data)

i named it resend_state.py

and then in the button:

double_tap_action:
  action: call-service
  service: python_script.resend_state
  service_data:
    entity_id: switch.roof

And its working on the 10th try… finally something that does what i want it to do

You wouldn’t even need python for this, but if it works now :man_shrugging: