If you have any Windows 10 or 11 PC at all . . . you can use PowerShell_ise to write text code. In fact the best use of PowerShell is to have it write more PowerShell code. Anyway . . .
I just thought someone might find this helpful as a methodology for creating a few sensors for each room where common sensors exist in each room. The below code saves me writing out 500 lines of template sensors manually and it’s super easy to generate new template sensors for each room if needed by altering the strings in the here statement.
Note: this approach needs a strongly consistent naming convention between the hardware entity names and the UI code.
function fg {$args}
cls
$RoomList = fg back_hall front_hall dining_room drawing_room spare_room upstairs_loo upper_hall dressing_room etc...
$TextInfo = (Get-Culture).TextInfo
foreach ($room in $RoomList) {
$spl = $room.split('_')
$sss = $spl[0]
$ttt = $spl[1]
$sss = $TextInfo.ToTitleCase($sss)
$ttt = $TextInfo.ToTitleCase($ttt)
$myvar =
@'
# sss ttt #
- sensor:
- name: rrrrrr_current_temperature
unique_id: rrrrrr_current_temperature
unit_of_measurement: '°C'
icon: mdi:thermometer
state: >-
{% set status = state_attr('climate.rrrrrr', 'current_temperature') %}
{% if status is not none %}
{{ status }}
{% else %}
unavailable
{% endif %}
- sensor:
- name: rrrrrr_target_temperature
unique_id: rrrrrr_target_temperature
unit_of_measurement: '°C'
icon: mdi:thermometer
state: >-
{% set status = state_attr('climate.rrrrrr', 'status') %}
{% if status is not none and 'setpoint_status' in status and 'target_heat_temperature' in status.setpoint_status %}
{{ status.setpoint_status.target_heat_temperature | float(0) }}
{% else %}
unavailable
{% endif %}
'@
$myvar -replace 'sss',$sss -replace 'ttt',$ttt -replace 'rrrrrr',$room
}
Generates
# Front Hall #
- sensor:
- name: front_hall_current_temperature
unique_id: front_hall_current_temperature
unit_of_measurement: '°C'
icon: mdi:thermometer
state: >-
{% set status = state_attr('climate.front_hall', 'current_temperature') %}
{% if status is not none %}
{{ status }}
{% else %}
unavailable
{% endif %}
- sensor:
- name: front_hall_target_temperature
unique_id: front_hall_target_temperature
unit_of_measurement: '°C'
icon: mdi:thermometer
state: >-
{% set status = state_attr('climate.front_hall', 'status') %}
{% if status is not none and 'setpoint_status' in status and 'target_heat_temperature' in status.setpoint_status %}
{{ status.setpoint_status.target_heat_temperature | float(0) }}
{% else %}
unavailable
{% endif %}