Templates and behaviour replicated to many entities

Hello,

I control many radiators with Shelly Relays 2.5 with relay 1 as global on/off and get power metering, and relay 2 as “fil pilote” (French system to switch between Eco/Comfort/… modes: here power off means Comfort, power on means Eco). This allows me to switch between three modes (R1 off + R2 on or off => Off // R1 on + R2 on => Eco // R1 on + R2 off => Comfort) while still measure power consumption on the right wire.

I’ve defined a sensor to abstract this “complexity” and factor the two relays states into one value:

---
# Factors the two Shelly Relay 2.5 relays (on/off & pilot)
# into one state for the radiator
platform: template
sensors:
  radiateur_chambre_1_mode:
    value_template: >-
      {% if is_state("switch.radiateur_chambre_1_channel_1", "off") %}
        Arrêt
      {% elif is_state("switch.radiateur_chambre_1_channel_1", "on") %}
        {% if is_state("switch.radiateur_chambre_1_channel_2", "off") %}
          Confort
        {% elif is_state("switch.radiateur_chambre_1_channel_2", "on") %}
          Eco
        {% else %}
          Unknown
        {% endif %}
      {% else %}
        Unknown
      {% endif %}
    friendly_name: "Mode radiateur chambre 1"

But I need this behaviour to be replicated for many radiators :slight_smile:
Is there a way to use templates to define it for many entities? Or define somewhere a meta-template that is instantiated as many times as needed?

I have it here for a sensor but the same question would also apply to automations and input_select as I also use it for this entitiy.

Thanks a lot for your lights! :slight_smile:

  • Cheers!

There’s currently no way to do this, but something in this direction is in the works, they call them blueprints.

1 Like

You’ll have to copy-paste the entire configuration for each room sensor and change the room number in each one. I offer you this version which makes it slightly easier to change the room number within the template:

# Factors the two Shelly Relay 2.5 relays (on/off & pilot)
# into one state for the radiator
platform: template
sensors:
  radiateur_chambre_1_mode:
    friendly_name: "Mode radiateur chambre 1"
    value_template: >-
      {% set ch = '1' %}
      {% set mode = { 0: 'Arret', 1: 'Confort', 2: 'Unknown', 3: 'Eco' } %}
      {% set c1 = 1 if is_state('switch.radiateur_chambre_' ~ ch ~ '_channel_1', 'on') else 0 %}
      {% set c2 = 1 if is_state('switch.radiateur_chambre_' ~ ch ~ '_channel_2', 'on') else 0 %}
      {{ mode[c1+c2] if c1+c2 in mode.keys() else 'Unknown' }}

  radiateur_chambre_2_mode:
    friendly_name: "Mode radiateur chambre 2"
    value_template: >-
      {% set ch = '2' %}
      {% set mode = { 0: 'Arret', 1: 'Confort', 2: 'Unknown', 3: 'Eco' } %}
      {% set c1 = 1 if is_state('switch.radiateur_chambre_' ~ ch ~ '_channel_1', 'on') else 0 %}
      {% set c2 = 1 if is_state('switch.radiateur_chambre_' ~ ch ~ '_channel_2', 'on') else 0 %}
      {{ mode[c1+c2] if c1+c2 in mode.keys() else 'Unknown' }}

  radiateur_chambre_3_mode:
    friendly_name: "Mode radiateur chambre 3"
    value_template: >-
      {% set ch = '3' %}
      {% set mode = { 0: 'Arret', 1: 'Confort', 2: 'Unknown', 3: 'Eco' } %}
      {% set c1 = 1 if is_state('switch.radiateur_chambre_' ~ ch ~ '_channel_1', 'on') else 0 %}
      {% set c2 = 1 if is_state('switch.radiateur_chambre_' ~ ch ~ '_channel_2', 'on') else 0 %}
      {{ mode[c1+c2] if c1+c2 in mode.keys() else 'Unknown' }}

In case you’re curious, mode is based on this:

Channel 2 Channel 1 State
0 0 Arret
0 1 Confort
1 0 Unknown
1 1 Eco
1 Like

If you are ok with NodeRed you can create one flow then call it for every sensor. No copy-paste anymore

1 Like

That’s neat. Can you provide an example of how to do it?

check out action, action in and action out nodes

Not quite the Home Assistant-related example I was expecting but thanks for that.

As much as I encourage people to learn new things, I think it’ll take longer to learn and apply ActionFlows compared to the ~5 minutes to copy-paste-modify a few lines of YAML. Maybe it’s worth the investment of time because ActionFlows have widespread application in Home Assistant. Is that your experience with it?

I’m a programmer. For me repeating code is a sign to refactor while copy-paste is just quick&dirty solution.

Yeah, c&p seems to be easier/quicker. But maintaining such code is way more expensive and error prone. This is why it’s considered to be anti-pattern.

Question was if there is a way to not copy&paste. I gave one.

Action flows are as simply as other ones. You create a flow which starts and ends with action start and end nodes respectively and does whaever you need. Then you can call such flow from any place in NR feeding it with data, and consuming message from its output.

Given all the disadvantages you have identified for copy-paste, it would be really useful to see how you use ActionFlows to define multiple, nearly-identical entities. Can you provide an example for the OP of a flow that creates multiple Template Sensors?

OK so I have to say sorry at first, because I led us to confussion.

Initially I reacted to “I have it here for a sensor but the same question would also apply to automations” fragment, since automation can be created as single sub-flow, then used for each radiator.

Unfortunatelly it’s not so easy with entities. At first, Entity node has limitations: cannot be used in subflows, also cannot greate HA entities based on input (entity must be configured inside every Entity node).

IMO technically it could be possible (NR communicates with HA using own web-socket extension) but I don’t know if NR provides any tools for that. Mentioned Entity node cannot help AFAIK. Maybe using Javascript calling mentioned web-socket extention methods or directly calling HA API. But those are speculations only. So sorry for confusion.

1 Like

Thanks a lot to all of you for your swift answers!
I will start with an enhanced copy-paste as proposed but will definitely give a look at NodeRed which I don’t use today to keep code DRY (Don’t Repeat Yourself).

I was also considering implementing such a behaviour as a custom component in Python but unsure this pattern would be a great fit from a Home Assistant architecture perspective, what do you think? (like: identifying Shelly 2.5 devices flagged as “radiators” within the full set of entities, then for each of them build the relevant sensor and automations). Or is there a way to overload an existing integration (here the Shelly integration) to add custom behaviours, apart from patching the integration code itself?

Thanks again for you lights! :slight_smile:

Fab1

I was also considering implementing such a behaviour as a custom component in Python but unsure this pattern would be a great fit from a Home Assistant architecture perspective, what do you think?

Hello
In fact mqtt autodiscovery does similar thing: it creates HA entities from data organized into predefined pattern. Isn’t it the same what your custom component might do?
I wouldn’t say it’s bad approach. Especially if you have skills for that.

This is great to read (translate) the combination of states to the actual radiator mode.

But how could we also have a sensor or selector to help set the mode of the radiator using the mode name and it would put on or off the corresponding switchs.

Thanks in advance