Read time/date from KNX Bus

Hi all,
i am quite new to HA and just started to configure a bunch of sensors like temperature which works like a charm.

Now i wanted to additionally add my Enertex PSU Stats into HA. Beside “Pulse Resets” (works fine with DPT 7.001) i am struggling with reading the “Last time/date when power on” state which are DPT 10.001 for time or DPT 11.001 for date.

Unfortunately sensors do apparently not support those DPTs. Is there any chance i can read those group addresses somehow else? tried with text but couldn’t find the correct syntax…

text:
  - name: "Enertex PSU Letzter Spannungsausfall Zeit"
    state_address: "12/0/1"

Thanks in advance.

Cheers Dino

Hi :wave:!
As you noted, these DPT are currently not supported sensor types.
You may configure a knx_event and use it with a trigger based template sensor. It will yield the raw data which you can convert with a template.

Maybe your PSU can also send eg. a binary value on startup, so you can just see the last time (HA System time) that entity was updated - this should roughly yield the same information.

Then again, when your PSU fails, does your IP interface still work? If not, HA entities will turn “unavailable” and you have that information in your entities history.

Alternatively, if you know some Python, you could try to add support for these to the HA integration / xknx.

1 Like

Thanks a lot for your fast reply @farmio !

I really would love to support the xknx integration, but i am on Python tutorial level which i did a couple of years ago :blush:

Had a quick look and couldn’t find any Boolean startup value in the PSU. However, it can be configured to sent all of those during startup to the bus. These are the ones i would like to monitor:

In case the PSU fails the IP interface will definitely not work anymore. Actually i am chasing a knx bus shortage somewhere in my bus circuit and it seems that when it rains or the weather changes from time to time my bus gets crazy :frowning:

I will try the trigger based template senor attempt wiht knx_event. However, i read the documentation already a dozen of times, but couldn’t figure out how knx_event, knx_event.register will work together and what syntax i would need to use. Maybe it was because i never used those (new to me) trigger based template sensors. I will give it a try again…

You don’t need the register service if you add the GAs statically to your configuration.yaml knx: event:. (Register is intended to add those on runtime - eg. for blueprints).

The template sensor is also added to configuration.yaml. Template - Home Assistant
You’d use something like this in the sensor config.

trigger:
  - platform: event
    event_type: knx_event
    event_data:
      destination: "1/2/3"
...

There’s nothing that can’t be learned. If you are really interested, I’m happy to help. If you have questions, feel free to join xknx Discord server :space_invader:

1 Like

Great! I got the raw payload now. Now comes the tricky party i guess, to evaluate and assign the payload into useable timestamp/date formats… but that is for another day :wink:

However, i am not sure if my way is the easiest one in my yaml file or if it could even be done in less lines:

knx.yaml

event:
  - address:
    - "12/0/*" #GAs der Enertex PSU

configuration.yaml

template:    
  - trigger:
      platform: event
      event_type: knx_event
      event_data:
        destination: "12/0/1"
    sensor:
      - name: "Enertex PSU Last Power Outage-Time"
        state: "{{ trigger.event.data.data }}"
     
  - trigger:
      platform: event
      event_type: knx_event
      event_data:
        destination: "12/0/4"
    sensor:
      - name: "Enertex PSU Last Bus Reset-Time"
        state: "{{ trigger.event.data.data }}"

Cheers Dino

Looks fine to me.
Here is how that conversion is done in Python: https://github.com/XKNX/xknx/blob/5aa408850aeef5b6e6280905c61263fd0cb6bc13/xknx/dpt/dpt_time.py#L21

trigger.event.data.data is a list of integers (representing bytes). So for the first Byte use trigger.event.data.data[0] - that would equal raw[0] from the example.

Hi @farmio
i finally had a chance to continue on this and have a deeper look into it. Took me a while until i figured out that i needed to separate and create three sensors (like you wrote in your last post :innocent:)

So i finally my trigger event looks like this:

  - trigger:
      platform: event
      event_type: knx_event
      event_data:
        destination: "12/0/4"
    sensor:
      - name: "Enertex PSU Last Bus Reset-Time Day-hr"
        state: "{{ trigger.event.data.data[0] }}"
      - name: "Enertex PSU Last Bus Reset-Time min"
        state: "{{ trigger.event.data.data[1] }}"
      - name: "Enertex PSU Last Bus Reset-Time sec"
        state: "{{ trigger.event.data.data[2] }}"

However, i am struggling and running in circles since three evenings/nights with converting and exctracting the weekday and the hour from 'sensor.enertex_psu_last_bus_reset_time_day_hr'.

I understood your way how to do it in your Python script, but i cannot figure out how to accordingly template it into a new sensor as i couldn’t figure out how to do the AND operation and bitwise move in Jinja2. I even asked ChatGPT (which quite helped me a lot already ;-))

I tried several syntax in Templating, but without success e. g.

enertex_psu_last_bus_reset_weekday:
  friendly_name: "Enertex PSU Last Bus Reset Weekday"
  value_template: >-
    {% set day_hr_dec = states('sensor.enertex_psu_last_bus_reset_time_day_hr') | int %}
    {% set weekday = ((day_hr_dec and 0xE0) / 32) %}
    {% if weekday in [1,2,3,4,5,6,7] %}
      {{ {1: 'Monday', 2: 'Tuesday', 3: 'Wednesday', 4: 'Thursday', 5: 'Friday', 6: 'Saturday', 7: 'Sunday'}[weekday] }}
    {% else %}
      No day
    {% endif %}
    {% set day_hr = states('sensor.enertex_psu_last_bus_reset_time_day_hr') | int %}
    {% set weekday = ((day_hr and 0xE0) >> 5) %}
    {{ weekday }}
    {% set day_hr_dec = states('sensor.enertex_psu_last_bus_reset_time_day_hr') | int %}
    {% set day_hr_hex = '{:x}'.format(day_hr_dec) %}
    {% set weekday = ((day_hr_dec | int) and 0b11100000) // 256 %}

All of these no success unfortunately :frowning:

I just was thinking of calling it via a python script and using your operations from your script, but unfortunately i’m struggling with getting to work a python script accordingly…

Do you have any idea or hint how to realize this?

Last question: Isn’t the pythin coinversation you posted me the solution i would need? I mean, if i understand it correctly it is integrating the DPT 10.001 Time or not? :joy:

Thanks in advance.

Br Dino

Chatgpt :roll_eyes: Try searching for “Jinja bitwise operators” in normal search and you will quickly find that and is not what you want, but HA has a custom bitwise_and() filter.

And yes, the Python function is probably what you want, but ist is just not implemented to be used with incoming time.

@farmio This helped a lot. It was hard to find it, but with your hint it was suddenly just there under Templating - Home Assistant :stuck_out_tongue_closed_eyes:
Totally overlooked that one:
Filter value_one|bitwise_and(value_two) perform a bitwise and(&) operation with two values.

Works now:

{% set day_hr = states('sensor.enertex_psu_last_bus_reset_time_day_hr') | int %}
{% set weekday = (day_hr | bitwise_and(0xE0) / 32) %}

Ah ok, i thought that your from_knx class would be the one which is used for incoming time.

Thanks a lot. Now i can implement all those sensors accordingly.

:+1:

It could be. There is just no functionality for using it from HA.

@farmio May you help me on this again please? Since latest core release (or maybe it was already like this before but i didn’t recognize?) it seems that the trigger event is dropping warnings in the log when it is triggered:

2023-06-02 00:51:50.642 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: None has no element 0 when rendering '{{ trigger.event.data.data[0] }}'
2023-06-02 00:51:50.644 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: None has no element 1 when rendering '{{ trigger.event.data.data[1] }}'
2023-06-02 00:51:50.646 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: None has no element 2 when rendering '{{ trigger.event.data.data[2] }}'

Any idea how i can get rid of these warnings?

Trigger Event:

  - trigger:
      platform: event
      event_type: knx_event
      event_data:
        destination: "12/0/1"
    sensor:
      - name: "Enertex PSU Last Power Outage-Time day-hr"
        state: "{{ trigger.event.data.data[0] }}"
      - name: "Enertex PSU Last Power Outage-Time min"
        state: "{{ trigger.event.data.data[1] }}"
      - name: "Enertex PSU Last Power Outage-Time sec"
        state: "{{ trigger.event.data.data[2] }}"

However, it seems to work though…

The event is triggered by any telegram to the defined GA. GroupValueRead telegrams don’t have data, hence the data key is NoneNone[0] raises an exception.
The next GroupValueWrite should just work fine.

  • You could add telegramtype: GroupValueWrote to the trigger, but then would miss GroupValueResponse.
  • you could add a condition checking for telegramtype
  • you could just check if data.data is not None in a condition or just in your template.

@farmio Makes it clear. Thank you! Took me a while to find the correct syntax, but this is the solution:

template:
  # DPT 10.001 Time / 11.001 Date    
  - trigger:
      platform: event
      event_type: knx_event
      event_data:
        destination: "12/0/1"
    sensor:
      - name: "Enertex PSU Last Power Outage-Time day-hr"
        state: "{% if not trigger.event.data.data == None %} {{ trigger.event.data.data[0] }} {% endif %}"
      - name: "Enertex PSU Last Power Outage-Time min"
        state: "{% if not trigger.event.data.data == None %} {{ trigger.event.data.data[1] }} {% endif %}"
      - name: "Enertex PSU Last Power Outage-Time sec"
        state: "{% if not trigger.event.data.data == None %} {{ trigger.event.data.data[2] }} {% endif %}"

Hi :wave:!
Just a small update: there are time entities now - which should be supported by KNX soon.
Unfortunately these are bidirectional entities. Afaik there is no option to set them “read-only” like a sensor.

1 Like

So, date will follow soon.

1 Like