Exposing a boolean value to a KNX group address possible?

Hi,

I’m trying to expose a boolean value (night=0, day=1) to a KNX group address. It seems that the KNX component doesn’t know about boolean values.

The data point type of KNX is boolean, DPT 1.002 and configured to the GA 0/0/4, it’s an input object of a light switch.

I tried various ways, but the message stays the same: ConversionError description="invalid value type" device_name="input_boolean.input_day_night" value_type="<boolean / or 1.002>"

This is my config:

    knx:
      tunneling:
        host:IP
        port: 3671
        local_ip: my_local_ip
      state_updater: true
      expose:
        - type: 1.002
          entity_id: input_boolean.input_day_night
          address: '0/0/4'

Anyone got an idea?

Regards
Oli

It says right in the documentation that it can only expose sensors or time.

“KNX component is able to expose time or sensor values to KNX bus.”

So make a template sensor that converts your boolean to a 0 or 1.

# Example configuration.yaml entry
sensor:
  - platform: template
    sensors:
      day_night:
        value_template: "{{ 1 if is_state('input_boolean.input_day_night','on') else 0 }}"
1 Like

ok, I’ve missed that part :slight_smile: thank you, petro!

No problem, I’m guessing there must be some limitation within knx because booleans are typically supported by most things.

Sorry but i am having issues following this, could you give me some instruction for “dummies” ?
I have an automation chnaging my frontend based on sun position how could i now send a Night / Day telegramm to my Group addres ?

- id: light couch on
  alias: Couch an schierf owes Un 
  initial_state: true
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: '{{ state.attributes.elevation }}'
    below: -1
  condition:
    condition: and
    conditions:
#    - condition: state
#      entity_id: group.network_devices_mobile
#      state: not_home
    - condition: time
      after: '16:00:00'
      before: '23:00:00'
  action:
  - service: homeassistant.turn_on
    entity_id: switch.lampe_canape
  - service: switch.turn_on
    entity_id: switch.armoire_sam
  - service: switch.turn_on
    entity_id: switch.armoire_cuisine
  - service: notify.ios_iphoneadonno
    data:
      title: Light
      message: Switching on light living
  - service: frontend.set_theme
    data:
      name: darkcyan

So, I don’t use KNX. I’m not familiar with the lingo. Is the ‘group address’ that you are referring to going to be exposed to knx?

Yeah somthink like magenbrot showed:

 expose:
    - type: 1.002
      entity_id: input_boolean.input_day_night
      address: '0/0/4'

So if you are just trying to make a sensor to expose day/night to knx then do this:

sensor:
  - platform: template
    sensors:
      knx_day:
        value_template: >
          {{ 1 if state_attr('sun.sun', 'elevation') | float >= -1.0 else 0 }}

Then expose this to knx:

    knx:
      ... whatever your config is...
      expose:
        - type: 1.002
          entity_id: sensor.knx_day
          address: '0/0/4'

This will output 1 if it’s daytime and 0 if it’s not daytime based on what you want which is daytime is when the elevations is greater than negative 1 elevation.

thank you for your help. It didn’t work it somehow screwed up my ha to knx connection but don’t know how i gues i’m using the wrong thing … i shoudl use this : https://www.home-assistant.io/components/knx/#services

I’m not sure, I’m not versed in KNX. You may want to make a new post about your end goal.

Hi @adonno

I’ve got the same problem, I need to tell my MDT switch if its day or night. Maybe you could return true or false instead of 1 or 0?

hi @mrkwtz,

I solved the problem with node-RED and a sensor template in homeassistant.

- platform: template
  sensors:
    day_night_boolean:
      friendly_name: 'Tag und Nacht'
      value_template: "{{ 1 if is_state('sun.sun','above_horizon') else 0 }}"

In node-RED I’ve these nodes configured (with node-red-contrib-knx):

[{"id":"35e82bd6.fbe1d4","type":"poll-state","z":"383a9e0e.772842","name":"get day & night","server":"e5f0b9ee.52af3","updateinterval":"10","outputinitially":true,"outputonchanged":false,"entity_id":"sensor.day_night_boolean","x":120,"y":120,"wires":[["85ece576.b59d68"]]},{"id":"85ece576.b59d68","type":"function","z":"383a9e0e.772842","name":"translate to KNX","func":"var dstgad = \"0/0/4\" // Tag & Nacht GA\n\nvar knx = { payload: {\"dstgad\":dstgad, \"value\":msg.payload, \"dpt\":\"1\"} };\nreturn knx;","outputs":1,"noerr":0,"x":340,"y":120,"wires":[["d468210e.066ed","dbaab846.269a18"]]},{"id":"d468210e.066ed","type":"debug","z":"383a9e0e.772842","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":590,"y":60,"wires":[]},{"id":"dbaab846.269a18","type":"link out","z":"383a9e0e.772842","name":"send to KNX","links":["e0e53391.581f"],"x":535,"y":120,"wires":[]},{"id":"e5f0b9ee.52af3","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":false,"rejectUnauthorizedCerts":true}]
1 Like

There is a completely different approach to solve the OP’s problem. The fact that the expose method exists in the KNX component leads many users to believe that this is the only way to transport data from HA to the KNX bus. But it is not.

The right way to solve this task is to create a KNX switch in HA and bind it to your boolean KNX group address. Then you can simply write to the HA switch with switch.turn_on / switch.turn_off and the state change will be copied to the KNX bus.

switch:

  - platform: knx
    name: "Tag/Nacht"
    # --> HA entity_id will be "switch.tag_nacht"
    address: '0/0/4'

automation:

  - id: daynight_switch_to_day
    trigger:
    ...
    action:
      - service: switch.turn_on
        entity_id: switch.tag_nacht

  - id: daynight_switch_to_night
    trigger:
    ...
    action:
      - service: switch.turn_off
        entity_id: switch.tag_nacht

You may also check my new KNX cookbook. I am currently adding recipes therer and am just about to add the above way to control KNX switche, together with more info about coupling KNX and Hue over HA:

1 Like

I was unable to expose boolean values to KNX.
Reason seems to be: there is no valid type.
I found these “type” as valid:

However, using “DPT-1”, “1.001”, “1.002”, “binary_sensor”, “on_off”, “boolean” or anything else did not work and result in a complete KNX crash.

I made a workaround using “percent” as type, and 100 and 0 instead of 1 and 0.

Hi, how can i change the value of this sensor with HA?

The KNX expose feature only forwards state changes of an existing entity to KNX. So you’d change the state of the observed entity.

Hello,

I also configured a template and a sensor in Home Assistant for the day/night object in KNX/ETS.
I have a created a group address in ETS of type binary.

But when I changed the value from 0 to 1 or vice versa I only get this message in the HA log:

Could not expose sensor.knx_day state value "1" to KNX: <ConversionError description="value invalid" device_name="sensor.knx_day__state" feature_name="State" value="1"/>

Here are my sensor and template configs:

sensors.yaml

 - platform: template                                                        
    sensors:                                                                  
      knx_day:                                                                
        value_template: >                                                     
          {{ 1 if state_attr('sun.sun', 'elevation') | float >= -1.0 else 0 }}

knx.yaml

expose:
  - type: binary
    entity_id: sensor.knx_day
    address: '0/0/4'

Can anybody help?

Thanks!

Your value template creates a sensor with "1" as state. This is the string “1”, not a number (since states are strings).

One way to handle this would probably be to use a template binary_sensor instead of a template sensor. See Template - Home Assistant

Or expose the sun directly and use a value template to calculate a binary value of it. Something like (untested)

expose:
  - type: binary
    entity_id: sun.sun
    attribute: elevation
    address: '0/0/4'
    value_template: "{{ value >= -1 }}"
2 Likes

Thank you very much!

That did the trick: