(beginner) jinja2 => mqtt => zigbee radiators

Hi everybody,

[[UPDATE 1]] about querying the group, I found out that I can do this by using {{ states.group.testslider.attributes.entity_id }}; this will output ('input_number.slider1', 'input_number.slider2'). I’ll keep researching this while hopefully getting some input here as well :pray:

I guess what I am attempting to do is much easier in the US where most people have a thermostat and AC in their house that can be controlled; however, here (Germany) I only have one mobile AC - all other rooms are heated by stationary radiators (1-2 per room) and a handful of infrared heaters. I would like to both control as well as automate these radiators, which all have a zigbee controlled knob that measures current temperature as well as regulates each radiator. (sorry for the long intro)

Either way, that was just so that you understand what I am trying to achieve. Currently I do this with ioBroker and node-red, but I am in the process of switching entirely to home-assistant. Each radiator will be controlled via zigbee2mqtt.

My attempt so far

  • created two test inputs
slider1: 
  name: radiators/office
  initial: 18
  min: 5
  max: 28
  step: 1

slider2: 
  name: radiators/kitchen
  inital: 16
  min: 8
  max: 25
  step: 1

In templates, I can get the current status of each slider by using {{ states.input_number.slider<n>.state }}. So if I enter {{ states.input_number.slider1.state }}, I currently get 6, which is what I had set this slider to. So far, so good.

  • created a group in groups.yaml
testslider:
  name: My sliders
  entities:
    - input_number.slider1
    - input_number.slider2

Is it possible to do something like this (but not exactly like it, because it does not work) as an automation:

- id: 'someid'
  alias "publish each slider value"
  trigger: 
    {% for entity_id in group.testslider %} # so that I will get EACH new slider automatically
    # when change in {{ states.input_number.slider<n> }}

  action:
    service: mqtt.publish
    data:
      topic: "{{ name.entity_id }}/set" # for example "radiators/office/set"
      payload: "{{ states.input_number.slider<n> }}"
      qos: 2
      retain: true

I am aware that this is awful syntax, but I thought it might explain what I am trying to achieve; if not:
whenever any slider in that group changes it’s value, publish this value to “<that_sliders_name>/set”. So if I change slider1 from 6 to 12, then mqtt.publish {"topic": "radiators/office/set", "payload: 12, "qos": 2, "retain": true}.

While I could just manually write an automation for each radiator, I am sure that this can somehow be achieved with jinja2. Also, I would later like to do the same for incoming values (so if somebody manually changed a radiator from 6 to 12 -triggering it to publish this via mqtt on its own- I want to update the slider’s value accordingly. I couldn’t even figure out how to query each entity_id in that group, but I’ll continue researching; though hopefully somebody on here can help me with a simple example automation that I can build upon for what I actually need to do :slight_smile:
Thanks in advance for your help.

What you want seems like it should be possible. However, the Jinja2 templating engine is not available in the entire YAML document. It is only available in select, “templateable” fields within the document.

Only the core YAML syntax is available in fields not strictly set for templating.

You might be able to make the YAML for these automations less verbose using YAML Anchors or Aliases (https://medium.com/@kinghuang/docker-compose-anchors-aliases-extensions-a1e4105d70bd). However, for me, a better approach is to use either AppDaemon (my preference) or Node Red.

I’d love to be wrong about this, as it would greatly simplify the YAML I use with Home Assistant. However, in the 3+ years I’ve been using Home Assistant, I’ve yet to find a nicer way of doing what you’re asking.

Assuming we have two input_number entities:

input_number:
  slider1:
    name: office
    initial: 21
    max: 28
    min: 18
    step: 1
  slider2:
    name: bedroom
    initial: 21
    max: 28
    min: 18
    step: 1

we can use the following automation to publish their state-changes to:
radiators/office/set
radiators/bedroom/set

- alias: 'publish sliderX'
  trigger:
    - platform: state
      entity_id:
        - input_number.slider1
        - input_number.slider2
  action:
    - service: mqtt.publish
      data_template:
        topic: 'radiators/{{ trigger.to_state.attributes.friendly_name }}/set'
        payload: '{{ trigger.to_state.state | int }}'

I’ve tested it and it works.


EDIT
You can add qos and retain options to the automation’s action. The defaults are qos=0 and retain=false. I would advise against retaining values that are used to control a device.

2 Likes

Thank you. This works perfectly! @123

It seems like template is really a concept I should get into more. It seems very hard to understand from my beginners point of view, but I guess once you get it it is the key for everything in hass.

Excellent! Thank you for this!