klogg
(Klogg)
August 11, 2021, 10:07pm
1
I want to trigger a template sensor every time a particular script is runs.
If my script uses mode: queued
does a call_service
event get fired every time the script starts again?
Or is it fired only once when it is first called?
I assume in the case of mode: parallel
it is fired every time it is called?
In a nutshell I want one or more trigger sensors to update themselves so that every time the script runs they have the latest state.
I think the answers are yes and yes. Depending on conditions that is.
1 Like
klogg
(Klogg)
August 12, 2021, 10:38am
4
AndâŚ
I am testing this but cannot get this trigger to, erm, trigger.
Clearly I have something wrongâŚ
template:
- trigger:
- platform: event
event_type: call_service
event_data:
domain: script
service: turn_on
service_data:
- entity_id: script.test_tts
sensor:
- name: Test Sensor
state: >
{{ ["Triggered",
"Updated",
"New State"] | random }}
Any ideas?
May I ask how did you turn_on
the script? Are you calling the service of script.turn_on
or are you using the one from the UI / menu?
Just a thought, but I think you need to use script.turn_on
.
petro
(Petro)
August 12, 2021, 11:16am
6
if an automation is firing the script, just use automation.trigger. Otherwise the called service will be the script name or script.turn_on.
- platform: event
event_type: call_service
event_data:
domain: script
service: turn_on
service_data:
- entity_id: script.test_tts
- platform: event
event_type: call_service
event_data:
domain: script
service: test_tts
klogg
(Klogg)
August 12, 2021, 12:35pm
7
That didnât work either.
Iâd really rather not use the automation.trigger
as the script is called from tons of different places. Not only would that be a pain to change but also having the trigger in the sensor makes it all much more scalable and maintainable (IMO)
This is what I am testing with in case you have the time/inclination to try it for yourself.
(I added the third trigger for good measure and out of desperation )
I expected the persistent notification to change (almost) every time script.test_event_trigger
runs which I am doing from the Developer Tools, Services page. But it stays as âunknownâ.
script:
test_event_trigger:
sequence:
service: persistent_notification.create
data:
message: "{{ states('sensor.test_event_trigger') }}"
template:
- trigger:
- platform: event
event_type: call_service
event_data:
domain: script
service: turn_on
service_data:
- entity_id: script.test_event_trigger
- platform: event
event_type: call_service
event_data:
domain: script
service: test_event_trigger
- platform: event
event_type: call_service
event_data:
domain: script
service: script.test_event_trigger
sensor:
- name: Test Event Trigger
state: >
{{ ["Triggered",
"Updated",
"New State"] | random }}
petro
(Petro)
August 12, 2021, 12:37pm
8
Well thatâs your problem, thatâs invalid yaml
service: script.turn_on
data:
entity_id: script.test_tts
what youâre sending is invalid json and it should result in an error because it doesnât see the word entity_id
as a string
So, try it with
service: script.turn_on
data:
entity_id: script.test_tts
or
service: script.test_tts
service: script.turn_on
target:
entity_id: script.test_event_trigger
If you choose the target from the UI (drop-down options), the YAML looks like the above.
klogg
(Klogg)
August 12, 2021, 12:44pm
10
Fair point.
ButâŚ
I am not sure that is the issue.
And the âcorrectâ way
They both seem to work without error (even though I agree the first one shouldnât)
Neither trigger the sensor.
petro
(Petro)
August 12, 2021, 12:46pm
11
what errors are in the log not sure what âunknownâ is supposed to mean on that notification
petro
(Petro)
August 12, 2021, 12:48pm
12
I see test_event_trigger
and your script name test_tts
donât match
klogg
(Klogg)
August 12, 2021, 2:03pm
13
Yeah, Iâm really sorry thatâs my fault that they donât match. It was just as a result of simplifying the code for posting here and then running the original.
Everything I said still stands though
This works
service: script.test_event_trigger
These donât work
service: script.turn_on
data:
entity_id: script.test_event_trigger
service: script.turn_on
data: {entity_id: script.test_event_trigger}
service: script.turn_on
data: {"entity_id": "script.test_event_trigger"}
I must still have something wrong in the trigger that uses the turn_on
service.
Which is where I started.
Here is the actual code I ran to test
script:
test_event_trigger:
sequence:
service: persistent_notification.create
data:
notification_id: >
{{ now() | string }}
title: Script test_event_trigger called.
message: >
At {{ now().hour }}:{{ now().hour }}:{{ now().second }} the state of 'sensor.test_event_trigger' is:
{{ states('sensor.test_event_trigger') }}
template:
- trigger:
- platform: event
event_type: call_service
event_data:
domain: script
service: turn_on
service_data:
- entity_id: script.test_event_trigger
- platform: event
event_type: call_service
event_data:
domain: script
service: test_event_trigger
sensor:
- name: Test Event Trigger
state: >
{{ ["Triggered",
"Updated",
"New State"] | random }}
Can you try modifying the template trigger sensor as below?
template:
- trigger:
- platform: event
event_type: call_service
event_data:
domain: script
service: turn_on
service_data:
entity_id:
- script.test_event_trigger
sensor:
- name: Test Event Trigger
state: >
{{ ["Triggered",
"Updated",
"New State"] | random }}
klogg
(Klogg)
August 12, 2021, 2:12pm
15
Yes I can but no, Iâm afraid that didnât work either.
koying
(Chris B)
August 12, 2021, 2:56pm
16
Works for me, with your âactualâ code.
I suspect itâs pure luck, though.
Your notification and the actual state change of the sensor run in parallel. If the notification runs before the sensor is actually updates, youâll get unknown
klogg
(Klogg)
August 12, 2021, 9:54pm
17
Yes but run it more than once and it should show the updated state from the previous trigger/run.
And in any case,
koying:
Works for me
Are you sure?
How are you calling the script?
The template in the âactual codeâ has two triggers.
Using this works (every time)
service: script.test_event_trigger
Using this doesnât work.
service: script.turn_on
data:
entity_id: script.test_event_trigger
petro
(Petro)
August 13, 2021, 7:29pm
18
Itâs clearly working because youâre getting the notification, itâs just that your template sensor isnât populated in time for your script to have the info. So just build an automation that sends the message when the template is updated.
klogg
(Klogg)
August 13, 2021, 9:22pm
19
But if that was the case then the sensor would be populated (late) but running the script a second time would show the state that it was populated with from the first run, and then get updated (late) again.
Wouldnât it?
It doesnât. It stays forever âunknownâ
In any case, today it came to me that there is a much simpler way to achieve what I want to achieve
- trigger:
- platform: state
entity_id: script.test_script_turn_on
attribute: current
My sensor(s) now seem to be updated every time the script runs irrespective of how it is called or the mode it is running in.