Error running service from Automation

Hello,

I’m trying to call a certain service from automation and it doesn’t work.

My Automation:

- id: '1597432951135'
  alias: New TEST Automation
  description: ''
  trigger:
  - entity_id: input_number.boiler_timer
    from: '0.0'
    platform: state
  condition: []
  action:
  - data:
      message: Boiler on
    service: notify.mobile_app_mi_a1
  - data_template:
      entity_id: 'switch.switcher_kis_boiler'
      timer_minutes: '{{states(''input_number.boiler_timer'')|int}}'
    service: switcher_kis.turn_on_with_timer

Im getting that error in the Log:

Logger: homeassistant.components.automation
Source: helpers/script.py:798
Integration: Automation ([documentation](https://www.home-assistant.io/integrations/automation), [issues](https://github.com/home-assistant/home-assistant/issues?q=is%3Aissue+is%3Aopen+label%3A%22integration%3A+automation%22))
First occurred: 2:37:13 PM (2 occurrences)
Last logged: 2:39:11 PM

New TEST Automation: Error executing script. Unauthorized for call_service at pos 2: UnknownUser

Running HassOS 4.8

When running in automation actions other services that comes with HA such as “switch.turn_on” all works fine.

What am I missing?
Thanks!
Rans

This is not a home assistant service:

    service: switcher_kis.turn_on_with_timer

Thanks Tom,

Is there a way to run it from an automation?

Thanks

Run what?

It isn’t a thing.

If you go to developer tools, services, select switcher_kis.turn_on_with_timer from the list, and click ‘call service’. What do you get ?

1 Like

Huh. Well there you go. Learn something new every day.

Hi Francis,

When calling the service from the developer tab, all works as expected.

Also, when adding it to a script, it does run as expected, but, when calling this script from automation it still doesn’t work from the same reason…

Any ideas?
Thanks

Try it like this:

  - service: switcher_kis.turn_on_with_timer
    entity_id: switch.switcher_kis_boiler
    data_template:
      timer_minutes: "{{states('input_number.boiler_timer')|int}}"
    

Thanks Tom,

Now I’m getting that error:
New TEST Automation: Error executing script. Invalid data for call_service at pos 2: value should be a string for dictionary value @ data['entity_id']
tried to add quotes but still same error

now my automation action looks like this:

  action:
  - service: switcher_kis.turn_on_with_timer
    entity_id: 'switch.switcher_kis_boiler'
    data_template:
      timer_minutes: "{{states('input_number.boiler_timer')|int}}"

Thanks for the help so far

I don’t think it should matter but try taking the single quotes from around the entity_id.

  action:
  - service: switcher_kis.turn_on_with_timer
    entity_id: switch.switcher_kis_boiler
    data_template:
      timer_minutes: "{{states('input_number.boiler_timer')|int}}"

Hi Tom,

That didn’t help.
Maybe that error log will help to figure out:

## Log Details (ERROR)

Logger: homeassistant.components.automation.new_test_automation
Source: components/switcher_kis/__init__.py:91
Integration: Automation ([documentation](https://www.home-assistant.io/integrations/automation), [issues](https://github.com/home-assistant/home-assistant/issues?q=is%3Aissue+is%3Aopen+label%3A%22integration%3A+automation%22))
First occurred: 12:08:41 PM (1 occurrences)
Last logged: 12:08:41 PM

While executing automation automation.new_test_automation

Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/components/automation/__init__.py", line 430, in async_trigger await self.action_script.async_run(variables, trigger_context) File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 802, in async_run await asyncio.shield(run.async_run()) File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 183, in async_run await self._async_step(log_exceptions=False) File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 191, in _async_step await getattr( File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 380, in _async_call_service_step await service_task File "/usr/src/homeassistant/homeassistant/core.py", line 1308, in async_call task.result() File "/usr/src/homeassistant/homeassistant/core.py", line 1343, in _execute_service await handler.func(service_call) File "/usr/src/homeassistant/homeassistant/components/switcher_kis/__init__.py", line 143, in async_turn_on_with_timer_service await _validate_edit_permission( File "/usr/src/homeassistant/homeassistant/components/switcher_kis/__init__.py", line 91, in _validate_edit_permission raise UnknownUser(context=context, entity_id=entity_id, permission=POLICY_EDIT) homeassistant.exceptions.UnknownUser: UnknownUser

Hi Ran,
There is a validation in the code that is preventing from a script to run the switcher_kis services.
I have created a pull request with a code change to allow turn_on_with_timer from a script and hope it will be approved soon

Hi Yinonco,

Thanks a lot.
I will follow-up on that.

Cheers!

Ran, @rans
Did you solve this? How?

I happen to have the same issue with switch.switcher_kis_boiler.
The script is running perfectly from the UI but fails with the same error when I run it from the automation.

I’m tired of cold showers, it will be a great help if you can share your solution :snowflake:

Thanks!

@yinonco,
You mention that there is a validation in the code preventing from a script to run the switcher_kis service.
The thing is, that it does run from a script when I execute the script from the UI, but it fails if automation is trying to execute the script.

Is it the same issue that you pull request solves?

Can you please update if the PR is approved?
Can you please share a link to the PR?

Thanks!

@raviv,
Here is the PR link https://github.com/home-assistant/core/pull/39693
I’m still tracking it as well…
Hope it will be merged soon. :crossed_fingers:

Thanks, Ran!
It seems that this issue was solved in a different manner by @thecode and was approved 4 days ago.
This is the PR link: https://github.com/home-assistant/core/pull/45204

It is now in home-assistant:dev branch, I’m not sure what is the deployment cycle, I guess it is a short way to master.

Thanks, Ran @yinonco @thecode your contribution is highly appreciated!

It will be in the next Home Assistant version, 2021.2.0, planned to be released on 03-FEB.

1 Like

That’s great news, thanks!

1 Like