Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sensors with Multiple Values #6

Open
lanrat opened this issue Dec 5, 2021 · 3 comments
Open

Sensors with Multiple Values #6

lanrat opened this issue Dec 5, 2021 · 3 comments

Comments

@lanrat
Copy link

lanrat commented Dec 5, 2021

As per the Home Assistant MQTT Discovery Documentation, it is possible to define a single sensor that can define multiple values in a single payload (with multiple configs needed) by defineing a value_template for each that use the same state_topic.

As far as I can tell, this is not possible with this library. Is that correct? Would it be much work to add support for this?

https://www.home-assistant.io/docs/mqtt/discovery/#sensors-with-multiple-values

@plapointe6
Copy link
Owner

You may be able to do this with plapointe6/HaMqttConfigBuilder

Here how:

// Publish this to: homeassistant/sensor/sensorBedroomT/config
String generateConfigPayload1() {
  return HaMqttConfigBuilder()
    .add("device_class", "temperature")
    .add("name", "Temperature")
    .add("state_topic", "homeassistant/sensor/sensorBedroom/state")
    .add("unit_of_measurement", "°C")
    .add("value_template", "{{ value_json.temperature}}")
    .generatePayload();
}

// Publish this to: homeassistant/sensor/sensorBedroomH/config
String generateConfigPayload2() {
  return HaMqttConfigBuilder()
    .add("device_class", "humidity")
    .add("name", "Humidity")
    .add("state_topic", "homeassistant/sensor/sensorBedroom/state")
    .add("unit_of_measurement", "%")
    .add("value_template", "{{ value_json.humidity}}")
    .generatePayload();
}

// To generate { "temperature": 23.20, "humidity": 43.70 }
String generateValuesPayload(const double temperature, const double humidity) {
  return HaMqttConfigBuilder()
    .add("temperature", temperature)
    .add("humidity", humidity)
    .generatePayload();
}

I will replace HAMqttDevice by the new lib HaMqttConfigBuilder soon in my other projects.

@lanrat
Copy link
Author

lanrat commented Dec 8, 2021

Oh, cool!

What the main difference between HAMqttDevice and the new HaMqttConfigBuilder?

@plapointe6
Copy link
Owner

HaMqttConfigBuilder is simple and tiny. It is a tool to generate key/value pairs into a JSON string.
HAMqttDevice does more things on its own under the hood like setting the state topic and generating some key/value entries into the config automatically.

So, why replace HAMqttDevice with HaMqttConfigBuilder if HAMqttDevice does, in fact, more things?

  1. HaMqttConfigBuilder is lighter and more flexible.
  2. While HAMqttDevice does things in the background, it was not always clear which thing it does and when. Doing this the new way will put all config key/value pairs for your device into the main sketch at the same place. Noting is done in the background.

I will update my IoT devices soon and I will put more examples into HaMqttConfigBuilder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants