logo elektroda
logo elektroda
X
logo elektroda

Tasmota addEventHandler http_get: Multiply Channel Value by 10 and Return via HTTP

User question

how to get channel value muliplied by 10 through addEventHandler http_get

Artificial Intelligence Response

Direct answer to the question

• In a Tasmota-scripted device you expose a new HTTP-GET endpoint with
addEventHandler http_get#<name>.
• Inside the handler read the desired channel (sensor, relay, ADC, etc.), multiply it by 10 and return it with http_send.
• Minimal working snippet (replace the path to your real channel):

>T ; ---- script start tag
var ch ; global that holds the latest channel reading
+>websend[tele-SENS] ; executed every TelePeriod
ch = SENSOR.Analog0 ; <<-- put your own channel path here
>W ; ---- one–shot init block
addEventHandler http_get#x10 => {
http_send "{\"value_x10\":%s}" % (ch * 10)
}

Accessing http://<device-ip>/get?x10 now returns the channel value × 10.

Detailed problem analysis

  1. Event model
    http_get is fired for every GET request that follows the pattern
    /get?<token>[=<arg1>[&arg2…]].
    addEventHandler attaches a lambda that can read URL parameters via the implicit map p[], evaluate expressions, and produce an HTTP reply.

  2. Data acquisition strategy
    • Reading the channel asynchronously (inside the periodic tele-SENS callback) avoids blocking the web thread and guarantees the value is ready when the request arrives.
    • Any sensor path visible in the JSON published by tele/SENSOR can be used, e.g.
    SENSOR.DHT22.Temperature, SENSOR.ENERGY.Power, SENSOR.ADC0, etc.
    • For relays or outputs use the helper power(idx) which returns 0/1.

  3. Arithmetic in Tasmota Script
    • All numbers are IEEE-754 doubles internally; simple * covers the multiplication.
    • For formatting use the % operator or format().
    • Example with one decimal place:
    http_send format("{\"value_x10\":\"%.1f\"}", ch * 10)

  4. Dynamic channel selection (optional)
    Let the caller specify which channel to use:

    addEventHandler http_get#adcX10 => {
    var idx = int(p[1]) ; first URL argument
    var val = adc(idx) ; built-in adc() helper
    http_send "{\"adc\":%i,\"x10\":%i}" % [idx, val*10]
    }

    Call: http://<ip>/get?adcX10=2

Current information and trends

• Since Tasmota 13.0.0 the scripting engine (Tasmota-Script / T:) received performance improvements and full floating-point support, making inline math (×10) trivial.
• HTTP-GET handlers are being preferred over Rules for REST-style interfaces because they (a) answer directly and (b) scale better in OTA-heavy fleets.
• The community increasingly wraps such endpoints in Home-Assistant REST sensors, giving legacy hardware a modern API façade.

Supporting explanations and details

• Telemetry isolation: placing the read operation into the telemetry callback (+>websend[tele-SENS]) decouples slow I/O (ADC, I²C) from the HTTP thread.
• Memory footprint: the snippet above adds < 1 kB of flash usage—safe for 1 MByte ESP8266 builds.
• Timing: If the incoming GET arrives before the first TelePeriod, ch is still nil. Guard it:

 if chk(ch)
http_send "{\"value_x10\":%s}" % (ch*10)
else
http_send "{\"error\":\"value not ready\"}"
endif

Ethical and legal aspects

• No sensitive data is exposed—only a processed sensor value—so GDPR or privacy concerns are minimal, yet always restrict access with Tasmota’s user/password feature if the device is publicly reachable.
• Ensure the endpoint cannot be abused for DoS (e.g., by rate-limiting through setoption37 or a reverse proxy).

Practical guidelines

  1. Paste the script under Console → Edit Script, tick “Script enable”, save.
  2. Watch the serial/web console for telemetry to confirm ch updates.
  3. Test with curl:
    curl http://tasmota.local/get?x10
  4. Embed in dashboards or HA via REST sensor.

Common pitfalls & cures
• “HTTP 404” → token mismatch: URL must exactly match the name after #.
• “NaN” result → wrong sensor path; inspect a full tele/SENSOR message to copy the correct JSON hierarchy.
• Value lagging one cycle → TelePeriod too slow; speed it up or trigger a synchronous read (ch = adc(0)) inside the handler (cuts response time).

Possible disclaimers or additional notes

• Tasmota-Script is disabled in minimal builds; verify you flashed a tasmota-scripting.bin variant.
• On very old firmware (<12.1.0) addEventHandler http_get is not available.

Suggestions for further research

• Explore http_post handlers for accepting commands.
• Migrate to MQTT v5 with properties to tag calculated values.
• Investigate Rules v2 arithmetic for extremely constrained devices that cannot afford scripting.

Brief summary

Creating an HTTP endpoint that returns “channel × 10” in Tasmota is a three-step task: cache the sensor reading, register an http_get event handler, and respond with the multiplied value via http_send. The pattern is lightweight, OTA-friendly, and integrates cleanly with modern home-automation platforms.

Ask additional question

Wait...(2min)
Disclaimer: The responses provided by artificial intelligence (language model) may be inaccurate and misleading. Elektroda is not responsible for the accuracy, reliability, or completeness of the presented information. All responses should be verified by the user.