core
core copied to clipboard
Trigger Update Coordinator: Running script requires passing in a context
The problem
With the deprecation of the forecast attribute, I've created a template sensor for the daily maximum temperature (see yaml below). Every hour, I get this warning in my logs, without any further information:
2023-09-08 12:00:00.222 WARNING (MainThread) [homeassistant.helpers.script.trigger_update_coordinator] Trigger Update Coordinator: Running script requires passing in a context
What version of Home Assistant Core has the issue?
core-2023.9.0
What was the last working version of Home Assistant Core?
No response
What type of installation are you running?
Home Assistant Container
Integration causing the issue
Core
Link to integration documentation on our website
No response
Diagnostics information
No response
Example YAML snippet
template:
- trigger:
- platform: time_pattern
hours: "/1"
- platform: homeassistant
event: start
- platform: event
event_type: event_template_reloaded
action:
- service: weather.get_forecast
data:
type: daily
target:
entity_id: weather.knmi_home
response_variable: forecast
sensor:
- name: KNMI max temp vandaag
unique_id: knmi_max_temp_vandaag
state: "{{ forecast.forecast[0].temperature }}"
availability: "{{ is_number(forecast.forecast[0].temperature) }}"
Anything in the logs that might be useful for us?
2023-09-08 10:00:00.493 WARNING (MainThread) [homeassistant.helpers.script.trigger_update_coordinator] Trigger Update Coordinator: Running script requires passing in a context
2023-09-08 11:00:00.493 WARNING (MainThread) [homeassistant.helpers.script.trigger_update_coordinator] Trigger Update Coordinator: Running script requires passing in a context
2023-09-08 12:00:00.222 WARNING (MainThread) [homeassistant.helpers.script.trigger_update_coordinator] Trigger Update Coordinator: Running script requires passing in a context
Additional information
No response
Hey there @phracturedblue, @tetienne, @home-assistant/core, mind taking a look at this issue as it has been labeled with an integration (template
) you are listed as a code owner for? Thanks!
Code owner commands
Code owners of template
can trigger bot actions by commenting:
-
@home-assistant close
Closes the issue. -
@home-assistant rename Awesome new title
Renames the issue. -
@home-assistant reopen
Reopen the issue. -
@home-assistant unassign template
Removes the current integration label and assignees on the issue, add the integration domain after the command.
(message by CodeOwnersMention)
template documentation template source (message by IssueLinks)
I have exactly the same log! Did not find why. All I did was modifying my template sensor using trigger and action to use the new forecast service
Still there on 2023.9.2
Same here using service: calendar.list_events
.
My calendar sensor (similiar to service: weather.get_forecast
) is the only one with an hourly time pattern trigger. Since I created it, I receive an hourly error message as described above.
still there with 2023.9.3
And still here on 2023.10.0b2
same here, using the doc example
Still there with 2023.10.0
This warning is generated in homeassistant/helpers/script.py
:
async def async_run(
self,
run_variables: _VarsType | None = None,
context: Context | None = None,
started_action: Callable[..., Any] | None = None,
) -> ScriptRunResult | None:
"""Run script."""
if context is None:
self._log(
"Running script requires passing in a context", level=logging.WARNING
)
context = Context()
It appears the warning is unnecessary, or should only be logged at a more verbose level, since the lack of context is already being dealt with by creating an empty Context()
.
As a workaround, you can define an automation to generate a custom event for triggers that don't have context, then have the template sensor trigger from the custom event (which will have context):
automation:
- alias: "Refresh 1h"
trigger:
- platform: homeassistant
event: start
- platform: time_pattern
hours: "/1"
action:
- event: refresh_1h
template:
- trigger:
- platform: event
event_type: refresh_1h
- platform: event
event_type: event_template_reloaded
action:
- service: weather.get_forecast
data:
type: daily
target:
entity_id: weather.knmi_home
response_variable: forecast
sensor:
- name: KNMI max temp vandaag
unique_id: knmi_max_temp_vandaag
state: "{{ forecast.forecast[0].temperature }}"
availability: "{{ is_number(forecast.forecast[0].temperature) }}"
Still here on 2023.11.0b0
Just a note to indicate it is still existing on 2023.12.0b1 (which makes sense because the PR hasn't been merged yet)
Unfortunately, I discovered that creating a new context within the template component makes the logbook inaccurate. For that reason, I'm leaving the PR as a draft until I have more time to get back to it or someone else finishes what I've started.
Creating a new context causes it to blame any change in the template sensors on the first service called in the action
section, which is inaccurate. A fix I found for that is to also fire an event after we create a new context and define an async_describe_events
callback to format/display the event in the logbook. Then every time any trigger fires we would spam an additional logbook entry that says nothing but 'template block was triggered', which is less than ideal, but prevents the logbook from associating the state change with the call_service
event.
The context/logbook behavior isn't obviously documented and I'm not super familiar, so I'm having to make sense of it as I go. There may be a better way to solve the subsequent issues that isn't obvious to me.
Bump. Still in 2023.12 @doug-hoffman great you try to create a fix! To my understanding you want to add context inside the template integration. I think the issue is related to sun/time/date triggers are missing context. Related issue: https://github.com/home-assistant/core/issues/68047
And still existing on 2024.1.0b5 (so it also isn't fixed by anything else, is my point ;) )
I don't know the impact of providing a context for time/date/sun triggers, but just making the logging debug level would be fine for me as well (as it is working)
I have exactly the same problem which is spamming my log (thousands of listings within one day).
However, it's a bit easier for me to determine the origin, as my system is up-to-date and virgin and I have only set up 2 automations so far. The error appears with every automation that I generate.
Attached is the error that is displayed and the two automations I have running in HA (I don't think the automation that triggers the switch for the first time (activated) is responsible because this automation was installed when the error already appeared in the log file).
Weird, I have these loggings only for automations triggered by time, date or sun triggers, where I call a script. But you are using device - power triggers, and you don't call a script either. Did you check that the trace timestamps of the automations Match with the loggings?
@erkr
Strange, I only have these logs for automations triggered by time, date or sun triggers where I call a script. But you are using device power triggers and not calling a script either. Have you checked that the trace timestamps of the automations match the logs?
yes, these match. or rather, another disruptive factor can also be ruled out because, as I mentioned, I have only installed two automations so far. One is the notification when the washing machine is ready and the pressing of the helper.button (Status_Waschmaschine). and on the other hand the automation which presses the created helper.button (Status_Waschmaschine) as soon as the smart power plug detects a power of 10 W.
It is really very strange that such warnings are displayed. I have already discussed this problem on HA's Discord server, under the Automations section, and here I got the answer that I can ignore this warning for the time being, as this is only a warning. Very strange statement. It was also not possible to determine what was causing the error
Automations are not the only source. I.e template sensors with time triggers, where the action calls a script will cause these warnings as well. I have over hundred automations that don't cause them. Please check if the warnings continue when you temporary disable these 2 automations
Example of a template binary sensor that causes these warnings:
- trigger:
- platform: time
at: "00:00:05"
action:
- service: pyscript.check_maintenance_actions
data:
dashboardUrl: dashboard-kiosk
viewTitle: status
response_variable: response
binary_sensor:
- unique_id: maintenance
name: Maintenance
state: "{{ response.expired > 0 }}"
@hpoperator86 The warnings Are for calling a script without a context. Your automations don't call a script. So I really doubth they cause the warnings.
Just dropping a note to prevent this becoming stale, as it is still occurring on 2024.2.0b1
even on todays dev install... 2024.3.0.dev20240216 using this trigger template: https://github.com/TheFes/cheapest-energy-hours/blob/main/documentation/1-source_sensor.md
is this even looked into?
@hpoperator86 The warnings Are for calling a script without a context. Your automations don't call a script. So I really doubth they cause the warnings.
In my system, the warnings are only logged for trigger based templates that are using a service with a response variable. As in the opening post. has nothing to do with automations or scripts (although under the hood they may be related obviously)
As far as I know automations will, in the background, call the action
part as a script. The action
sequence from a trigger based template sensor will be the same. So that will be why the error states a script is running
@TheFes I have my doubts if @hpoperator86 took the right conclusion as he assumed the loggings where the result of automations. As @Mariusthvdb mentioned, I also do see the loggings for time triggered template sensors only! If automations like the ones included above would be the cause, I should be spammed. That is not the case.
@hpoperator86 would be great if you can confirm the loggings are gone if you disable your two automations
yes, and the trigger is important, because my weather template dos not throw the error when using:
template:
- trigger:
- platform: state
entity_id: sensor.date
# - platform: event
# event_type: event_template_reloaded
# - platform: homeassistant
# event: start
action:
- service: weather.get_forecasts
target:
entity_id: weather.buienradar
data:
type: daily
response_variable: buienradar_forecast
I only experienced the warning after installing this:
template:
- trigger:
# - platform: time_pattern
# hours: '/1'
- platform: homeassistant
event: start
action:
- service: easyenergy.get_energy_usage_prices
data:
incl_vat: false
config_entry: 382801d5dbf1bebc969f1fa7fb2e31c6
start: >
{{today_at() - timedelta(days=1)}}
end: >
{{today_at() + timedelta(days=2)}}
response_variable: prices
with either (or all) trigger. So even homeassisant start triggers the warning, which would hardly constitute a 'timing' trigger.
My setup is as follows and I also get these warnings:
template:
- unique_id: homepod
trigger:
- platform: webhook
webhook_id: "SECRET_HERE"
allowed_methods:
- POST
local_only: true
action:
- service: input_boolean.turn_off
target:
entity_id: input_boolean.homepod_update
I have a time-based automation that turns input_boolean on to initiate the update and the template sensor above to capture the results. Disabling the automation and toggling the input manually results in the same warning.
While this is open, I changed the corresponding automation in HomeKit to turn off the input_boolean instead. When I remove the action, the warnings disappear.
Hi all
Accidentally I found out the "action" clause seems crucial (as suggested by @TheFes), when I refactored this sensor (no loggings):
# Template NOT causing missing context loggings:
- trigger:
- platform: time_pattern
minutes: /5
binary_sensor:
- unique_id: check_windows
name: Check windows
state: |
{%- from 'tools.jinja' import check_window %}
{{ check_window()|bool }}
device_class: problem
Into this variation with the action clause (causing context loggings):
# Template sensor causing the missing context logging (every 5 minutes):
- trigger:
- platform: time_pattern
minutes: /5
action:
- variables:
status: |
{%- from 'tools.jinja' import check_window %}
{{ check_window()|bool }}
binary_sensor:
- unique_id: check_windows
name: Check windows
state: "{{ status }}"
device_class: problem
Note: Functionally, these two versions are identical. But only the second one causes the loggings.
Best Eric
@jaydeethree You are right, Changing to the "trigger" as in your "solution" , doesn't throw errors On the other hand, it doesn't update either, as it seems the weather-integration doesn't make calls to the Provider-Api, when they update information ( Maybe the weather-integration only makes 1, at best 2 calls a day , to the cached data, from the i.e met.no integration) Below changes as in your "Fix" is not reliable, as in an hour the data is outdated, and not updated
template:
- trigger:
- platform: state
entity_id: weather.home
action:
- service: weather.get_forecasts
data:
On the other hand, yes the example in Weather-Integration page, does throw an error ( I have no idea how this example could end up in the Docs ) PS: i use met.no as provider
Im baffled about how a homeautomation system, can even "implement" such an unreliable weather-integration, since the "turnover" to service-call for weather. The default weather card, was previous announced that changes been made so the integration should provide 6 days instead of 5, but fact is , the card shows 5( Where As 1 is actually current, most of the hour, so it leaves us with 4 days forecast. in the default card, using met.no ) Forecast are as known "unreliable" therefore Providers updates there forecasts at-least every hour, cause i.e the Wind Changes, more rain than expected, colder doo to wind-change etc.
I Hope Devs will reconsider this Integration, as weather-forecast IS an Important feature in a homeautomation system, im pretty sure noone using HA haven't got the weather in their installation. Everyone has ! ( They rely upon it( as much as one can ) , but most are not ware about the actual "glitch" and unreliable facts, because they don't compare. But it seems like Devs forcing people into template-sensors, automations and own-made direct Api-calls , as currently one can not rely upon this implementation of a weather-forecast, to control anything. And the "still" constant " Trigger Update Coordinator: Running script requires passing in a context " even with the example in Docs, is unbelievable This is i assume, your suggested solution applied to Example in Docs, using met.no.
- trigger:
- platform: state
entity_id: weather.forecast_home
action:
- service: weather.get_forecasts
data:
type: hourly
target:
entity_id: weather.forecast_home
response_variable: hourly
sensor:
- name: Temperature forecast next hour
unique_id: temperature_forecast_next_hour
state: "{{ hourly['weather.forecast_home'].forecast[0].temperature }}"
unit_of_measurement: °C
PS: i added " availability: "true" ", just for a test, also tried with the suggested " is_number " above ( However not in an automation ) , only for this example sensor