Tuya wifi temperature & humidity configuration

Configuring and Using Cheap Tuya Sensors with Home Assistant

Here's my way to configure and use those cheap Tuya sensors. I hope it will help someone without tedious trial and error :)

Sensor Overview

The Tuya sensor is simple and uses 2 AAA batteries. It has a button and provides temperature and humidity readings via WiFi.

Steps to Configure the Sensor

  1. Install Batteries

    Insert the batteries; the device should start to blink and become discoverable in the Smart Life app. If the device isn't detected, press the button (or use a paperclip to press the S/S pin). In case of issues, remove ALL batteries and retry.

  2. Connect to WiFi

    Give the device your WiFi credentials. You should see the device added and readings displayed. Rename the device by clicking on the pen icon.

  3. Set Up in Home Assistant

    Ensure you have LocalTuya installed in Home Assistant. In "Configure," select "Add new device." Find the device with the name you set and select "Configure all recognized devices automatically." If you didn't find the device, hit "Reload" on LocalTuya.

  4. Handling Device Sleep Mode

    The device sleeps to save battery, waking up to transmit data only if readings change or after an hour. To configure, wake it by pushing the button and retry adding it if needed.

  5. Configure the Device

    Once added, go to Configure and select Modify. Remove unnecessary PIDs, keeping temperature, humidity, and battery. Adjust scaling factors: temperature to 0.1, humidity to 1, and battery to 1. Check readings (e.g., something like 23.5°C, 53%).

  6. Define a Template to Retain Values

    The following entity id would be sensor.th1a_temperature (that is a different thing from unique_id)
    template:
      - sensor:
          - name: "th1a Temperature"
            unique_id: "th1a_temperature"
            state_class: "measurement"
            device_class: "temperature"
            unit_of_measurement: "°C"
            attributes:
              area: "{{ area_name('sensor.th1a_temperature') }}"
            state: >
              {% set initial_value = 0 %}
              {% if not states('sensor.1a_temperature_humidity_temperature') | is_number %}
                  {% if states('sensor.1a_temperature') in [None, 'unknown', 'unavailable'] %}
                    {{ initial_value }}
                  {% else %}
                    {{ states('sensor.1a_temperature') | float(0) }}
                  {% endif %}
              {% else %}
                {{ states('sensor.1a_temperature_humidity_temperature') | float(0) }}
              {% endif %}
          - name: "th1a Humidity"
            unique_id: "th1a_humidity"
            device_class: "humidity"
            state_class: "measurement"
            unit_of_measurement: "%"
            attributes:
              area: "{{ area_name('sensor.th1a_humidity') }}"
            state: >
              {% set initial_value = 0 %}
              {% if not states('sensor.1a_temperature_humidity_humidity') | is_number %}
                {% if states('sensor.1a_humidity') in [None, 'unknown', 'unavailable'] %}
                  {{ initial_value }}
                {% else %}
                  {{ states('sensor.1a_humidity') | float(0) }}
                {% endif %}
              {% else %}
                {{ states('sensor.1a_temperature_humidity_humidity') | float(0) }}
              {% endif %}
    
  7. Handle Restart Issues

    Use an SQL query to retrieve the last sensor value before a restart. Update the template to use these values:

     template:
      - sensor:
          - name: "th1a Temperature"
            state: "{{ states('sensor.1a_temperature_humidity_temperature_last') | float(0) }}"
          - name: "th1a Humidity"
            state: "{{ states('sensor.1a_temperature_humidity_humidity_last') | float(0) }}"
    

    sql:

    • name: “1a_temperature_humidity_temperature_last”
      unique_id: “1a_temperature_humidity_temperature_last”
      query: >
      SELECT COALESCE ((
      SELECT CAST(states.state AS REAL) as state
      FROM states
      LEFT JOIN state_attributes ON (states.attributes_id = state_attributes.attributes_id)
      WHERE metadata_id = (
      SELECT metadata_id FROM states_meta WHERE entity_id = ‘sensor.1a_temperature_humidity_temperature’
      ) AND states.state IS NOT NULL AND NOT states.state in ( ‘unavailable’, ‘unknown’) AND CAST(states.state AS REAL) > -100
      ORDER BY states.state_id DESC
      LIMIT 1), -100.0) AS state;
      column: “state”

    • name: “1a_temperature_humidity_humidity_last”
      unique_id: “1a_temperature_humidity_humidity_last”
      query: >
      SELECT COALESCE ((
      SELECT CAST(states.state AS REAL) as state
      FROM states
      LEFT JOIN state_attributes ON (states.attributes_id = state_attributes.attributes_id)
      WHERE metadata_id = (
      SELECT metadata_id FROM states_meta WHERE entity_id = ‘sensor.1a_temperature_humidity_humidity’
      ) AND states.state IS NOT NULL AND NOT states.state in ( ‘unavailable’, ‘unknown’) AND CAST(states.state AS REAL) > -100
      ORDER BY states.state_id DESC
      LIMIT 1), -100.0) AS state;
      column: “state”

With those data you can display on a grafana, and display also the area of the sensor (without having to name the sensor with the area in the name) . In the following influx query I get data from all temperature sensor

from(bucket: “YourBucket”)
|> range(start: 0) // Retrieve all data from the start
|> filter(fn: (r) => r.entity_id =~ /^th.*_temperature$/)
|> yield(name: “latest_record_all_fields”)

These steps should help you set up and use the Tuya sensors effectively in Home Assistant without tedious trial and error. If you have any further questions or need additional assistance, feel free to ask!