Resend switch state on hold_action:(solved)

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
  action: toggle
  action: call-service
  service: switch.turn_off
    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:

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:

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 }'switch', 'turn_off', service_data)
    service_data = {'entity_id': entity_id }'switch', 'turn_on', service_data)

i named it

and then in the button:

  action: call-service
  service: python_script.resend_state
    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: