I have set up a light that is operated by a button that toggles the light on or off. It works okay but have a problem in that it sometimes requires more than one click to operate. I can also operate the light by a manual light switch. I believe that using the manual switch changes the ‘state’ of the switch and the app has to reset it before it can work.
What I am attempting to do is put some ‘if then else’ logic into the code so that the app knows the ‘state’ of the switch and then switches it accordingly. I have put the logic into a script but it does not seem to be able to find it.
First you shouldn’t need to test the state to decide whether to turn the switch on or off. You should just be able to use the switch.toggle service. If that doesn’t work then what you’re trying to do won’t work either.
Next, if you’ve entered the script properly (and reloaded scripts, or restarted HA), it should show up, at a minimum, on the STATES tab of the Developer Tools page. It should also show up in the list of services on the SERVICES tab. Do you see it listed in either of those places? If not, then either you’ve entered it incorrectly (in which case there should probably be an error shown somewhere in the logs), or you haven’t reloaded scripts or restarted HA.
It would also help to post the entire script, not just a snippet. If you entered it incorrectly that would make it easier for us to spot the problem.
Thanks for your reply. The light I am switching can be switched either manually(wall switch) or using HA. If I use HA it often takes two click to switch it. I believe this is because I have used the wall switch and HA is out of sync with the wall switch. I.e. If I have switched the light off using the wall switch and HA still has the state as on. If I then try to switch the light on using HA all HA does is try to switch it off. I think HA does change the state to off, then on the next click on HA the light will come on.
Can you understand my explanation. I realise it’s not the end of the world if I have to do two clicks but I am just trying to get it right.
As for the second part, I was writng the code in the ui page and it doesn’t show all the code to copy to the topic text. I’ll do it again in the configuration.yaml file and see how it goes.
If you entered the script via the UI then that’s probably the issue behind your first problem. When you create a script via the UI the name you enter is not actually used for calling the script like you’re trying to do, its only used as the alias. For example, I created a script via the UI just now and here’s what goes in my scripts.yaml file:
'1590540061582':
alias: New Script
sequence:
- data: {}
service: alert.toggle
Notice that the alias is called “New Script” but before that there is this random string of numbers before the colon. That is the identifier of this script and that’s what I have to use when I want to call this script using a service call action (like what you’re trying to do from the button).
You can see this really easily if you go into developer tools and go to the service tab. Use the dropdown to help you see everything in the system, when I do that after making my script I see this:
As you can see, I don’t see script.new_script as an option instead I see script.1590540061582 which matches up with that randomly generated string of numbers in my scripts.yaml file.
I don’t really know why it does this but as a general practice what I usually do when I want to use the UI is first make my script via the UI, then ssh in and open my scripts.yaml file in order to edit that ID (and add input variables and anything else I want to do). Then finally go to server controls and choose “reload scripts”. After that you can continue using the UI if you want (it won’t reset the ID again). But now you can actually use your script. If you only use the UI your scripts end up pretty unusable unfortunately. At least for anything other then being manually run from the UI.
For your second problem, what is the integration you’re using for the lights? With light integration typically the state of the lights is kept up to date in home assistant. So if you use an external switch like then then HA sees and recognizes that change so it knows it flipped from on to off, even though that change in state wasn’t actually done through HA.
I understand your problem but I’m not understanding yet why its occurring. We might be able to provide more help if you provide more info on how your lights are integrated with HA and why the state is not up to date in HA when toggled externally.
Thanks for the heads up on the id problem. I did a bit of programming in java and php a few tears ago but I new to this and am getting a bit lost.
If I create code in the configuration file does it have to go anywhere special or do I just head it with ‘script’, ‘automation’ or what ever it is.
I have two lights setup so my wife(who has a mobility problem) can switch them with her iPad. I have HA setup on a raspberry pi 4 and have a z wave dual nano switch to control the lights. Both lights have the same problem but I am just trying to fix one at a time.
For the one that I have not tried to change I use a button card configuration, the code is as follows:-