Extract xy value from a sensor attribute

Hi everyone.
I’m trying to integrate the various functions that the zigbee remote control has, and I’m stuck on some things.
The device in question is: https://www.zigbee2mqtt.io/devices/MLI-404011.html integratet with Zigbee2Mqtt, and works correctly.
Now I would like to extract the xy attributes that I receive when I press, to be able to use it on the action of an automation.


Actually, with the help of a friend @Alesoft73 i did it, and it works fine. But the problem is that every time I get an error on the log. Here’s what we did.

  - alias: Tint Colori Led TV
    mode: parallel
    trigger:
    - platform: state
      entity_id: sensor.tint_action
      to: 'color_wheel'
    action:
    - service: light.turn_on
      data_template:
        entity_id: light.led_tv
        xy_color: >
          [{{ state_attr("sensor.tint_action","action_color") | regex_replace("([xy:{}\-\/@])")| regex_replace("'") | replace (" ","")}}]

and this is the error

Error while executing automation automation.tint_colori_led_tv: None for dictionary value @ data['xy_color']

Tint Colori Led TV: Error executing script. Invalid data for call_service at pos 1: None for dictionary value @ data['xy_color']

how can i solve? thanks

Wow that seems long winded

Do any of these work ?

{{ states.sensor.tint_action.attributes.action_color }}
{{ states.sensor.tint_action.attributes.action_color.x }}
{{ states.sensor.tint_action.attributes.action_color.y }}
1 Like

1st template result:

{
  "x": 0.123
  "y": 0.456
}

2nd and 3st results:

UndefinedError: 'mappingproxy object' has no attribute 'action_color'

obviously it doesn’t work in automation

Where is that result coming from? What’s making that entity? That looks like a dictionary but it’s not because the comma is missing, meaning that’s a string response with carriage returns. This seems like a bug in whatever is creating that entity. So, if you created the MQTT topic for that information, you forgot the comma. Once you add that in, these should work:

{{ states.sensor.tint_action.attributes.action_color.x }}
{{ states.sensor.tint_action.attributes.action_color.y }}

I think it’s coming from

{{ states.sensor.tint_action.attributes.action_color }}

but the output is not valid JSON (as you rightly pointed out)

It is an integrated remote control on Zigbee2Mqtt, https://www.zigbee2mqtt.io/devices/MLI-404011.html which exposes me in Home Assistant via autodiscovery.
I show you the results i receive on the Z2M log and on the sensor in HA.
Z2M log:

MQTT publish: topic 'zigbee2mqtt/Tint', payload '{"action":"color_wheel","action_color":{"x":0.12,"y":0.08},"action_group":16388,"linkquality":86,"rate":100,"transition_time":0}'

Sensor in HA:

I meant ‘what’s creating the entity’. It seems like it’s MQTT, but that’s a guess because MQTT discovery would have formatted it properly. My guess is that he manually configured the MQTT device.

No, is not manually. But Mqtt Discovery.

Yes, but the data getting sent from zigbee2mqtt to HA is what you want to look at. So what does the topic contain when zigbee2mqtt writes to the topic from the device?

This is the log on zigbee2mqtt:

MQTT publish: topic 'zigbee2mqtt/Tint', payload '{"action":"color_wheel","action_color":{"x":0.12,"y":0.08},"action_group":16388,"linkquality":86,"rate":100,"transition_time":0}'

What interests me is to obtain the xy values ​​to be used in automation (look 1st post):

    action:
    - service: light.turn_on
      data_template:
        entity_id: light.led_tv
        xy_color: [x.xxx,y.yyy]

I hope I have been better explained. Greatings

@_dev_null @petro do you have any ideas? Thanks

Hi Ingrid,

The problem I have is that your published data appears to be correct but the output from sensor.tint_action is not valid.

I would have debugged the mqtt topic by subscribing to zigbee2mqtt/Tint but fear that will be valid so my hunch is firmly in the way that sensor.tint_action is being populated or modified.

Something has been lost in translation whilst populating that sensor. Do you have the definition for this sensor that you can provide - maybe in configuration.yaml or is it automatically created ?

Considering I’ve also found this post it may be a good time to post all the related sensors and configuration and explain exactly what you’re trying to achieve rather than trying to solving the problem piecemeal which can lead to further problems down the road :+1:

Cheers

1 Like

The sensor is populated automatically, with Z2m autodiscovery.
The json in zigbee2mqtt is correct {“x”: 0.118, “y”: 0.197}.
Instead in home asssitant it is imported incorrectly.

You can see it from how you set Entity from Home Assistant.

x_y

So for now the only way to fix and have no errors is to filter and split the entity.
So:

    xy_color: >-
      {% set color = state_attr("sensor.tint_action","action_color")|string %}
      {% if color.split(",")[0] | regex_replace ("x|y|:|{|}|\s|'")|float != 0 %}
      [{{ color.split(",")[0] | regex_replace ("x|y|:|{|}|\s|'")|float }}, {{ color.split(",")[1] | regex_replace ("x|y|:|{|}|\s|'")|float}}]
      {% endif %}

The result is :[0.xxxx, 0.xxxx]
Thanks

1 Like