MQTT AutoGenerated Light "Unknown"

Hello! I’m attempting to build an automation for connecting an RVC Canbus network to Home Assistant by way of a script that processes the canbus messages and loads them onto an MQTT broker configured to support Home Assistant Device AutoDiscovery.

I’ve got it to the point that Home Assistant recognizes the device, but it’s not properly recognizing state. Can someone help?

Here’s the messages being received:

Topic: homeassistant/light/master_bath_ceiling_light/config

{
  "brightness_value_template": "{{ value_json.brightness }}",
  "command_topic": "homeassistant/light/master_bath_ceiling_light/set",
  "name": "Master Bathroom Ceiling Light",
  "brightness_command_topic": "homeassistant/light/master_bath_ceiling_light/set",
  "payload_on": "ON",
  "brightness_state_topic": "homeassistant/light/master_bath_ceiling_light/state",
  "payload_off": "OFF",
  "value_template": "{{ value_json.state }}",
  "unique_id": "master_bath_ceiling_light",
  "state_topic": "homeassistant/light/master_bath_ceiling_light/state",
  "brightness_scale": 255,
  "brightness_command_template": "{{ value }}",
  "brightness": true,
  "json_attributes_topic": "homeassistant/light/master_bath_ceiling_light/state",
  "device_class": "light"
}

And here’s the topic: homeassistant/light/master_bath_ceiling_light/state

{
  "state": "ON",
  "brightness": 38
}

Any thoughts?

1 Like

Amature here, struggeling with mqtt discovery myself.
Are you aware that your brightness_state_topic and your state_topic
are exactly the same.
I could imagine that it is ambiguous (for HA) what entity you mean when you publish to that topic.
[/quote]

Hey thanks for the response!

I am. I had also tried splitting it out thinking that it might be part of the issue, but it didn’t seem to matter.

I believe these two values tell HASS what to look for in that state topic:

"brightness_value_template": "{{ value_json.brightness }}"

And

"value_template": "{{ value_json.state }}"

It also appears that hass does infact see the state changes, it just doesn’t process them for some reason? Logs;

024-08-20 10:42:11.777 DEBUG (MainThread) [homeassistant.components.mqtt.models] Rendering incoming payload '{"state":"ON","brightness":36}' with variables {'entity_id': 'light.master_bath_ceiling_light', 'name': 'Master Bathroom Ceiling Light', 'this': <template TemplateStateFromEntityId(light.master_bath_ceiling_light)>} with default value 'default' and Template<template=({{ value_json.brightness }}) renders=2873>
2024-08-20 10:42:13.777 DEBUG (MainThread) [homeassistant.components.mqtt.client] Received message on homeassistant/light/master_bath_ceiling_light/state (qos=0): b'{"brightness":36,"state":"ON"}'
2024-08-20 10:42:13.777 DEBUG (MainThread) [homeassistant.components.mqtt.models] Rendering incoming payload '{"brightness":36,"state":"ON"}' with variables {'entity_id': 'light.master_bath_ceiling_light', 'name': 'Master Bathroom Ceiling Light', 'this': <template TemplateStateFromEntityId(light.master_bath_ceiling_light)>} with default value 'default' and Template<template=({{ value_json.brightness }}) renders=2874>
2024-08-20 10:42:15.777 DEBUG (MainThread) [homeassistant.components.mqtt.client] Received message on homeassistant/light/master_bath_ceiling_light/state (qos=0): b'{"brightness":36,"state":"ON"}'
2024-08-20 10:42:15.777 DEBUG (MainThread) [homeassistant.components.mqtt.models] Rendering incoming payload '{"brightness":36,"state":"ON"}' with variables {'entity_id': 'light.master_bath_ceiling_light', 'name': 'Master Bathroom Ceiling Light', 'this': <template TemplateStateFromEntityId(light.master_bath_ceiling_light)>} with default value 'default' and Template<template=({{ value_json.brightness }}) renders=2875>

But for some reason it just doesn’t process it. It still shows up as an unknown state in developer tools.

Missing state_value_template ?

afbeelding

You get this as state

but this is defined as state on

Trying to wrap my head around this…

So I’ve told HASS via the payload_on option that my light will be considered ON when the value is “ON”. But by default, HASS is assuming the state field will be ‘power on’ or ‘power off’? So payload_on and payload_off are just for setting the command, not for the state interpretation? Hence why I need to define the state_value_template to interpret “ON” as on and not expect “power on” for determining state?

This worked by the way. :slight_smile: Thank you!!!

Am I understanding correctly that, you are sending the on/off_state and the brightness_state as a set (of key:value pairs) to the same topic?
And then tell HA (by the first line in your _config topic) to use the .brightness value for the brightness and the .state value for the on/off state?

Interesting :slight_smile:

(Don’t worry explaining in too much detail… I will only need that in 2027 I think. Not fully grasping the use of templates yet.)

Yup! That’s exactly it.