I am trying to control multiple lights via script; the script itself works fine when using just one light, so there must be something wrong with the way I am trying to add multiple lights. Can you please tell me what I am doing wrong?
NOTE:
The smallest increment of time in Home Assistant is 1 second. Therefore there’s no need to indicate precision beyond a whole value. In other words, this:
- delay:
seconds: 1.00
is unnecessary because Home Assistant only handles whole values (integers) like this:
2019-10-07 14:56:31 ERROR (MainThread) [homeassistant.components.automation] Error while executing automation automation.wecker_wochenende. Invalid data for call_service at pos 1: extra keys not allowed @ data['wecklicht']
The way I understood it, I could use any data when passing on to a script, as long as I then callthat data (so if I used wecklicht as a variable, I’d have to call wecklicht from a script). However, that seems to be the problem here. I cannot use wecklicht, or my template is wrong so that it does not recognize it as a variable…?
not sure if that will work as a variable. I believe it would come across as a string. Only a few fields know how to take a comma separated string and turn it into a list. That’s dependent on the service and usually only for the entity_id field.
If these don’t transfer objects into the script and they transfer comma separated strings, then go with @123’s config and then separate the entities out inside your variable
{{ wecklicht.split(', ') }}
change your service to this:
- service: script.sonnenaufgang
data:
wecklicht:
You’re getting that error because the service script.turn_on does not accept random values. You have to call scripts directly to get around this. Or place wecklicht inside variables when using script.turn_on.
Good point! I had made that change in my version without considering how light.turn_on may differ from homeassistant.turn_on (assuming homeassistant was only a more comprehensive version).
I can confirm it does NOT work if the script uses homeassistant.turn_on. That service can’t handle a comma-delimited string of entity_id’s.
Thank you so much! It works perfectly now
I don’t remember why I had used homeassistant.turn_on for this to begin with, but I remember something didn’t work back when I first created this script (a few version updates ago) when I used light.turn_on
I agree. I’ve been banging my head to get it working using homeassistant.turn_on or off and now I see this doesn’t work. I also had some issues using a template sensor holding a list of devices that needed interaction on. It appears if you use data_template in a script, this needs to be converted into a string. Lost a day sorting this out. Only way to get it working was to use {{states.sensor.something.state | string}}
states are always strings no matter what. Shouldn’t have to cast it as a string at all. Attributes on the other hand can be anything. But even in the end, casting it as a string isn’t really required because all jinja templates are strings when the template is finalized.
Maybe it’s because I created a template sensor with a tuple? The sensor presents the data as string in the FE, but without the conversion in my script it doesn’t work. It throws an error stating 'not a valid valid value for dictionary value @ data[‘entity_id’].
Furthermore in the same script I had to change the service homeassistant.turn_on (that switches lights and input_booleans into two different services, light.turn_on and input_boolean.turn_on). Homeassistant.turn_on threw an error. Very strange.
The () must from the tuple must be screwing with the template. if you change your sensor.something’s output from a tuple to a comma separated string {{ blah | join(', ') }}, then that would probably remove the need for the | string at the end. But that still doesn’t make sense because any .state is already a string.
This fact has recently raised a question (to myself) which I’d like an informed answer to rather than my assumption (that HA just deals with it by hoping the string can be converted on the fly).
numeric_state triggers.
The state is always a string so how does it do the above or below comparison?
Surprisingly I have never used a numeric_state. It must have flown under the radar when I first started with HA and I only recently stumbled upon them! I’ve always used templates.
You can convert any string to a number by casting it and having fail safes when you cannot cast it. This is most likely built into the logic behind the trigger.
triggers outside templates all handle the string states safely. It’s just template triggers where the user has to properly use filters to cast types. For example, the float filter has this safety built in. It will take any string and attempt to convert it to a number. If it fails, it converts it to zero. So the user will always have a number after it’s cast.