If you create a complicated sensor template and want to reuse it, you have to cut-n-paste the whole thing and modify a few variables. It could be so much cleaner if it was possible to create either a blueprint for sensors. Alternatively enabling the standard Jinja templating features: include or import.
Sensor blueprints would make sharing and installing complicated sensors easier, especially for users not willing or able to edit jinja and/or YAML.
So I mean you can reuse if you switch to a trigger template entities since then you can use trigger variables combined with yaml anchors. For example I have a set of sensors like this:
template:
- trigger:
- platform: state
variables: &basement-trigger-variables
presence_entities: &basement-presence-entities
- binary_sensor.basement_camera_person_alert
entity_id: *basement-presence-entities
to:
binary_sensor:
name: Person detected basement
unique_id: c38933d24a2a478cab11d0e1ae3279fe_detector_basement
device_class: occupancy
state: &detector-state >-
{{ expand(presence_entities | default([])) | selectattr('state', 'eq', 'on
') | list | count > 0
or is_state('binary_sensor.awake_at_home', 'on') and
expand(awake_presence_entities | default([])) | selectattr('state', 'eq'
, 'on') | list | count > 0
}}
attributes: &detector-attributes
entity_id: >-
{{ presence_entities | default([]) +
(awake_presence_entities | default([]) if is_state('binary_sensor.awak
e_at_home', 'on') else []) }}
- trigger:
- platform: state
variables: &bedroom-trigger-variables
presence_entities: &bedroom-presence-entities
- binary_sensor.bedroom_monitor_in_use
- binary_sensor.bedroom_motion_sensor_occupancy
- binary_sensor.bedroom_tv_in_use
awake_presence_entities: &bedroom-awake-presence-entities
- switch.bedroom_closet_light
entity_id: *bedroom-presence-entities
to:
- platform: state
variables: *bedroom-trigger-variables
entity_id: *bedroom-awake-presence-entities
to:
- platform: state
variables: *bedroom-trigger-variables
entity_id: binary_sensor.awake_at_home
to:
binary_sensor:
name: Person detected bedroom
unique_id: 7585346108f14c3194e94a3396c1d96b_detector_bedroom
device_class: occupancy
state: *detector-state
attributes: *detector-attributes
... Other rooms
But it would be nice if this was easier.
Also I definitely wish you could put template sensors in blueprints. Or better yet, blueprint packages.
Yes, I think a blueprint package, where you can add more than just automations would be really cool. Adding template sensors, scenes and or scripts would make sharing of more complex automated stuff easier.
Yes the macros are really handy. It helps to make my code cleaner. However we still cannot share this in a blueprint. Therefore some kind of blueprint package would be nice to have, a way to share not only automations but also macros and template sensors for example