Template Usage

Hi,
I have a question about the use of templates, I have a serial sensor that connects to the USB port on the KNX bus of my home domotics system and I am able to view the telegrams that pass through the BUS.
I need to intercept some telegrams that identify the status of the lights on or off.
I was able to intercept the ON and OFF switch-on telegrams of a light and when the telegram passes it switches to the True state, to then reposition itself immediately in the False state.
Below is the template:

        friendly_name: "Living Room Light ON"
        value_template:>
          {{'0D C1 E1 00 80' in states ('sensor.serial_sensor'). Split (':') [1]}}
        friendly_name: "Living Room Light OFF"
        value_template:>
          {{'0D C1 E1 00 81' in states ('sensor.serial_sensor'). Split (':') [1]}}

Examples of telegrams and template output:

KNX Telegram: B0 10 0C 0D C1 E1 00 80 FE

        friendly_name: "Living Room Light ON"
        value_template:>
          False
        friendly_name: "Living Room Light OFF"
        value_template:>
          True

I would like to understand if It is possible to set the last state to True to indicate the last state of the light.
Can anyone tell me how to do it?
Thanks in advance.

Hi!
I’d suggest to use the knx integration for this. It needs a Knx-IP-Interface. If you don’t have one you can use knxd to emulate one from your USB-Interface. (Search the forum here for knxd).
You won’t have to handle telegram payloads then and Entities are created for you.

Hi Matthias,
thank you for reply!
IP KNX Gateway are too expensive, I have a serial knxgate that permit my self to manage automation/light/scene with an interface that cost less than €30, IP GW for my System cost more than € 1000.
Thanks to HA I’m already can control with the Alexa Integration perfectly, what I need is to use template and set switch status to have every time the right states of my switches.
Thanks!

Where are you using these templates?

I’m testing in the Developer Tools section to understand if all goes well, then I will move to configuration.yaml.

As I said, you can emulate an IP Interface with knxd. It costs nothing.
Normal Knx Interfaces cost ~120€

This one?

Thank you guys explaining why I can get more using an IP Interface, maybe in future I will move to them.
I also know busware an itneresting project!

Now I’d like to understand if someone knows how to solve using HA templates or by other software way!

It depends where you are using it. for example if in a template switch with an entity id of switch.my_switch

  value_template: >-
    {% if '0D C1 E1 00 80' in states ('sensor.serial_sensor'). Split (':') [1] %}
      {{'ON'}}
    {% elif '0D C1 E1 00 81' in states ('sensor.serial_sensor'). Split (':') [1] %}
      {{'OFF'}}
    {% else %}
      {{states('switch.my_switch') | upper}}
    {% endif %}

There are better ways to do this that scale more efficiently for more entities. See the strategies here:

you should be able to turn your serial/usb interface into an IP interface with my add-on that has been linked above. You can then also use ETS5 via IP and what not.

edit: so basically install the add-on (or run knxd on your device of choice), adjust the configuration to the KNX interface device you currently have and enjoy all the benefits of an IP interface from than on. No need for the busware adapter etc - just use what you have. It works with serial interfaces, USB interfaces and what not. For more details check out the knxd github pages

Thank you for your help!
Seems to be the right way, I tried the code but I need to tune a little bit cause sensor return after few second in off state.
I’ll try go deeper with the guide above.

Interesting Add-ons I’ll try asap to understand if I will be anle to obtain better control on my bus.
Do you have also test if is It possibile to control Security Alarms or activation/disactivation of the Security System?

I don’t have a KNX based security system, but via the services and features provided by the HomeAssistant KNX component (xknx) you can also send custom packages/commands to the KNX bus - so you should be good to go in case it requires some special handling.

By default xknx (the KNX component of HA) does not forward group messages to HA that are not mapped to entities, but you can easily tell it to do so and then use HA logic/scripts/templates/what not to listen to these group messages and do what you want. I wrote myself a small AppDeamon app which allows me to control my HUE lights via KNX switches and show their states and colour on my KNX switches (they have a display). So the possibilities are basically endless and you still have all the freedom you want, you only get a loooooot more convenience since all the heavy lifting with the bus communication is handled in the background for your.

If you are not using that code in a template switch it wont work. You can’t use the template editor. The template relies in the sate of the switch, switch.my_switch to keep the state after telegrams have been received.