petro
(Petro)
December 1, 2024, 12:23pm
1
Right now, template triggers will only trigger when the template itself switches from resolving False to resolving True.
Template entities update whenever the template updates, why can’t we have a trigger that does the same thing?
User87
(User87)
December 1, 2024, 12:57pm
2
I’m not the brightest bulb in the group, so you probably understand something deeper about HA than I. But, can you not use binary template helpers as triggers for automations? I have a few of these and use ‘not’ statements to inverse the ‘false → true’ all the time.
{% set varA = is_state('binary_sensor.xyz','off') %}
{% set varB = is_state('binary_sensor.abc','on') %}
{% set varC = is_state('binary_sensor.def','off') %}
{{ (varA or varB) and not varC }}
petro
(Petro)
December 1, 2024, 1:00pm
3
See this closed PR for reference on what type of templates this effects
home-assistant:dev
← Petro31:value-template-trigger
opened 04:53PM - 07 Jul 24 UTC
<!--
You are amazing! Thanks for contributing to our project!
Please, DO N… OT DELETE ANY TEXT from this template! (unless instructed).
-->
## Proposed change
<!--
Describe the big picture of your changes here to communicate to the
maintainers why we should accept this pull request. If it fixes a bug
or resolves a feature request, be sure to link to that issue in the
additional information section.
-->
Currently, template triggers only trigger when the template previously renders `false` and then renders `true`. This limits template triggers and forces users to create template entities.
Adding the ability to optionally trigger off any template change opens up functionality for automations and template entities.
This solves 2 commonly asked questions that haven't been possible in the past:
e.g. A user would now be able to create a trigger based template sensor where there's shared templates across states and attributes.
```
template:
- trigger:
- platform: template
value_template: "{{ states.binary_sensor | map(attribute='entity_id') | select('is_state_attr', 'device_class', 'battery') | select('is_state', 'on') | list }}"
trigger_on_new_value: True
sensor:
- name: Binary Sensor On Entities
state: "{{ trigger.value | count }}"
attributes:
entity_id: "{{ trigger.value }}"
```
People can trigger off entities inside a group. e.g. You can now trigger when any light is turned on (or off) in a group. In this scenario, `trigger.value` will provide the list of entities that are on. Users can discern whether or not the trigger.entity_id was turned on or off to perform specific actions.
```
trigger:
- platform: template
value_template: >
{{ expand('light.living_room') | map(attribute='entity_id') | select('is_state', 'on') | list }}
condition:
- platform: template
value_template: >
{# Only perform the action if the triggering entity was turned on (it should be in trigger.value if it was) #}
{{ trigger.entity_id in trigger.value }}
action:
...
```
The options here are endless because we can utilize templates to enact any trigger.
This can also be combined with `label_entities`, `device_entities`, `integration_entities`, and any other `*_entities` that I might be forgetting. The only downside to using these functions is that the template will not update if you add or remove a label, a restart of the system is required. This stipulation should be added to the documentation or a separate PR should be created to allow templates to render when the lists change.
e.g.
```
trigger:
- platform: template
value_template: "{{ label_entities('living_room') | select('is_state', 'on') | list }}"
```
## Type of change
<!--
What type of change does your PR introduce to Home Assistant?
NOTE: Please, check only 1! box!
If your PR requires multiple boxes to be checked, you'll most likely need to
split it into multiple PRs. This makes things easier and faster to code review.
-->
- [ ] Dependency upgrade
- [ ] Bugfix (non-breaking change which fixes an issue)
- [ ] New integration (thank you!)
- [x] New feature (which adds functionality to an existing integration)
- [ ] Deprecation (breaking change to happen in the future)
- [ ] Breaking change (fix/feature causing existing functionality to break)
- [ ] Code quality improvements to existing code or addition of tests
## Additional information
<!--
Details are important, and help maintainers processing your PR.
Please be sure to fill out additional details, if applicable.
-->
- This PR fixes or closes issue: fixes #
- This PR is related to issue:
- Link to documentation pull request: https://github.com/home-assistant/home-assistant.io/pull/33614
## Checklist
<!--
Put an `x` in the boxes that apply. You can also fill these out after
creating the PR. If you're unsure about any of them, don't hesitate to ask.
We're here to help! This is simply a reminder of what we are going to look
for before merging your code.
-->
- [x] The code change is tested and works locally.
- [x] Local tests pass. **Your PR cannot be merged unless tests pass**
- [x] There is no commented out code in this PR.
- [x] I have followed the [development checklist][dev-checklist]
- [x] I have followed the [perfect PR recommendations][perfect-pr]
- [x] The code has been formatted using Ruff (`ruff format homeassistant tests`)
- [x] Tests have been added to verify that the new code works.
If user exposed functionality or configuration variables are added/changed:
- [x] Documentation added/updated for [www.home-assistant.io][docs-repository]
If the code communicates with devices, web services, or third-party tools:
- [ ] The [manifest file][manifest-docs] has all fields filled out correctly.
Updated and included derived files by running: `python3 -m script.hassfest`.
- [ ] New or updated dependencies have been added to `requirements_all.txt`.
Updated by running `python3 -m script.gen_requirements_all`.
- [ ] For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
<!--
This project is very active and we have a high turnover of pull requests.
Unfortunately, the number of incoming pull requests is higher than what our
reviewers can review and merge so there is a long backlog of pull requests
waiting for review. You can help here!
By reviewing another pull request, you will help raise the code quality of
that pull request and the final review will be faster. This way the general
pace of pull request reviews will go up and your wait time will go down.
When picking a pull request to review, try to choose one that hasn't yet
been reviewed.
Thanks for helping out!
-->
To help with the load of incoming pull requests:
- [ ] I have reviewed two other [open pull requests][prs] in this repository.
[prs]: https://github.com/home-assistant/core/pulls?q=is%3Aopen+is%3Apr+-author%3A%40me+-draft%3Atrue+-label%3Awaiting-for-upstream+sort%3Acreated-desc+review%3Anone+-status%3Afailure
<!--
Thank you for contributing <3
Below, some useful links you could explore:
-->
[dev-checklist]: https://developers.home-assistant.io/docs/development_checklist/
[manifest-docs]: https://developers.home-assistant.io/docs/creating_integration_manifest/
[quality-scale]: https://developers.home-assistant.io/docs/integration_quality_scale_index/
[docs-repository]: https://github.com/home-assistant/home-assistant.io
[perfect-pr]: https://developers.home-assistant.io/docs/review-process/#creating-the-perfect-pr
User87
(User87)
December 1, 2024, 1:10pm
4
Got it! So you’re looking for non-binary changes to a template.